﻿<?xml version="1.0" encoding="utf-8"?>
<ConnectionManager Name="Intellum" RuntimeVersion="" Help="http://www.cozyroc.com/ssis/intellum-connection" xmlns="http://www.cozyroc.com/schema/rcm-config-1.0.xsd">
	<Service EndPoint="https://[Host].exceedlms.com/api/v3" />

	<Authentication Name="JWT Auth">
		<Documentation>https://datatracker.ietf.org/doc/html/rfc7523</Documentation>

		<User>
			<Parameter Name="PrivateKey" Type="file">
				<Documentation>Required. Select private key file (p12/pfx).</Documentation>
			</Parameter>
			<Parameter Name="PrivateKeyPassword" Type="password">
				<Documentation>Required. The password for the PFX file.</Documentation>
			</Parameter>
			<Parameter Name="AppUID">
				<Documentation>Required. Specify the oauth app uid.</Documentation>
			</Parameter>
			<Parameter Name="Scope" Default="admin_read admin_write">
				<Documentation>Required. Specify the desired scope.</Documentation>
			</Parameter>
			<Parameter Name="Audience">
				<Documentation>Required. Specify the audience with no trialing slash.</Documentation>
			</Parameter>
		</User>

		<Test Url="/courses" />

		<Token Url="{{=new Uri(connection.serverHost).authority}}/oauth2/token.json" Method="POST" Result="{{=response.access_token}}">
			<Parameters>
				<Parameter Name="application/json" Value="{{=
				{
					grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
					assertion: Intellum.JWT.generateJWT()
				} }}" Type="Body" />
			</Parameters>

			<Refresh Url="{{=new Uri(connection.serverHost).authority}}/oauth2/token.json" Method="POST" Result="{{=response.refresh_token}}" Expiration="{{=Date.now() + (response.expires_in - 300) * 1000}}">
				<Parameters>
					<Parameter Name="application/json" Value="{{=
					{
						grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
						assertion: Intellum.JWT.generateJWT()
					} }}" Type="Body" />
				</Parameters>
			</Refresh>
		</Token>

		<Parameters>
			<Parameter Name="Authorization" Value="Bearer {{=token.Access}}" Type="HttpHeader" />
		</Parameters>
	</Authentication>

	<Resources>
		<Template>
			<Field Name="ShortText" DataType="DT_WSTR" Length="250" />
			<Field Name="LongText" DataType="DT_WSTR" Length="1000" />
			<Field Name="DateTime" DataType="DT_DBTIMESTAMP" />
			<Field Name="Date" DataType="DT_DBDATE" />

			<Resource Name="Base">
				<Read>
					<Parameters>
						<Parameter Name="page" Value="{{=parameters.iterator}}" />
						<Parameter Name="records_per_page" Value="{{=parameters.batchSize}}" />
						<Parameter Name="_includeUserParameters" Value="{{=parameters}}" />
					</Parameters>

					<Iterator>
						<Next Value="{{=parseInt(parameters.iterator || 0) + 1}}" />
					</Iterator>
				</Read>

				<Create Method="POST">
					<Parameters>
						<Parameter Name="application/json" Value="{{=item}}" Type="Body" />
					</Parameters>
				</Create>

				<Update Method="PUT">
					<Parameters>
						<Parameter Name="application/json" Value="{{=item}}" Type="Body" />
					</Parameters>
				</Update>

				<Delete Method="DELETE" />

				<Upsert Method="PATCH">
					<Parameters>
						<Parameter Name="application/json" Value="{{=item}}" Type="Body" />
					</Parameters>
				</Upsert>
			</Resource>
		</Template>
	</Resources>
	<Script>
		<Module Name="Main">
			<![CDATA[
require('underscore');
require('Uri');

var Intellum = {};

]]>
		</Module>
		<Module Name="JWT Auth">
			<![CDATA[
require('oauth2.jwt');

Intellum.JWT = {};


///////////////////////////////////////////////////////////////////////////////
Intellum.JWT.generateJWT = function() {
	var jwt1 = {
		alg: 'RS256'
	};

	var now = Math.floor(Date.now() / 1000);

	var jwt2 = {
		iss: connection.user.AppUID,
		scope: connection.user.Scope,
		aud: connection.user.Audience,
		iat: now,
		exp: now + 120
	}

	return JWT.createAssertion(jwt1, jwt2, connection.user.PrivateKey, connection.user.PrivateKeyPassword);
}
]]>
		</Module>
	</Script>
</ConnectionManager>