﻿<?xml version="1.0" encoding="utf-8"?>
<ConnectionManager Name="NICE CXone" RuntimeVersion="" Help="http://www.cozyroc.com/ssis/nice-cxone-connection" xmlns="http://www.cozyroc.com/schema/rcm-config-1.0.xsd">
	<Service EndPoint="https://api-[cluster].incontact.com" />

	<Authentication Name="Client Credentials">
		<Documentation>https://developer.niceincontact.com/API/AuthenticationAPI#/Token/getToken</Documentation>

		<User>
			<Parameter Name="client_id">
				<Documentation>Required. Specify client_id.</Documentation>
			</Parameter>
			<Parameter Name="client_secret" Type="password">
				<Documentation>Required. Specify client_secret.</Documentation>
			</Parameter>
			<Parameter Name="access_key" Type="password">
				<Documentation>Required. Specify access key.</Documentation>
			</Parameter>
			<Parameter Name="access_secret" Type="password">
				<Documentation>Required. Specify access secret.</Documentation>
			</Parameter>
		</User>

		<Test Url="/agents" />

		<Token Url="https://cxone.niceincontact.com/auth/token" Method="POST" Result="{{=response.access_token}}">
			<Parameters>
				<Parameter Name="grant_type" Value="password" />
				<Parameter Name="username" Value="{{=connection.user.access_key}}" />
				<Parameter Name="password" Value="{{=connection.user.access_secret}}" />
				<Parameter Name="Authorization" Value="Basic {{=Base64.encode(connection.user.client_id + ':' + connection.user.client_secret)}}" Type="HttpHeader" />
			</Parameters>

			<Refresh Url="https://cxone.niceincontact.com/auth/token" Method="POST" Result="{{=response.refresh_token}}" Expiration="{{=Date.now() + (response.expires_in - 900) * 1000}}">
				<Documentation>https://developer.niceincontact.com/API/AuthenticationAPI</Documentation>

				<Parameters>
					<Parameter Name="grant_type" Value="refresh_token" />
					<Parameter Name="Authorization" Value="Basic {{=Base64.encode(connection.user.client_id + ':' + connection.user.client_secret)}}" Type="HttpHeader" />
					<Parameter Name="refresh_token" Value="{{=token.Refresh}}" />
				</Parameters>
			</Refresh>
		</Token>

		<Parameters>
			<Parameter Name="Authorization" Value="Bearer {{=token.Access}}" Type="HttpHeader" />
		</Parameters>
	</Authentication>

	<Authentication Name="OAuth">
		<Documentation>https://developer.niceincontact.com/API/AuthenticationAPI</Documentation>

		<User>
			<Parameter Name="client_id">
				<Documentation>Required. Identifier of application that is associated with the authentication token.</Documentation>
			</Parameter>
			<Parameter Name="redirect_uri">
				<Documentation>Required. Specify the redirect_uri of your application.</Documentation>
			</Parameter>
		</User>

		<Test Url="/agents">
			<Parameters>
				<Parameter Name="top" Value="1" Type="HttpHeader" />
			</Parameters>
		</Test>

		<Token Url="{{=new Uri(connection.serverHost).authority()}}/auth/authorize" Result="{{=response.access_token}}">
			<Documentation>https://developer.niceincontact.com/API/AuthenticationAPI</Documentation>

			<Parameters>
				<Parameter Name="response_type" Value="code" />
				<Parameter Name="client_id" Value="{{=token.client_id}}" />
				<Parameter Name="redirect_uri" Value="https://www.cozyroc.com/oauth_callback" />
				<Parameter Name="scope" Value="openid" />
				<Parameter Name="nonce" Value="{{=Math.random()}}" />
			</Parameters>

			<Authorized Id="https://www.cozyroc.com/oauth_callback" Result="{{=connection.execute({
				url: new Uri(connection.serverHost).authority() + '/auth/token',
				method: 'POST',
				parameters: {
					grant_type: 'authorization_code',
					code: response.code,
					client_id: token.client_id,
					client_secret: token.client_secret,
					redirect_uri: token.redirect_uri
			} })}}" />

			<Refresh Url="https://login.microsoftonline.com/{{=token.tenant}}/oauth2/v2.0/token" Method="POST" Result="{{=response.refresh_token}}" Expiration="{{=Date.now() + (response.expires_in - 300) * 1000}}">
				<Documentation>https://developer.niceincontact.com/API/AuthenticationAPI</Documentation>

				<Parameters>
					<Parameter Name="grant_type" Value="refresh_token" />
					<Parameter Name="refresh_token" Value="{{=token.Refresh}}" />
					<Parameter Name="client_id" Value="{{=token.client_id}}" />
				</Parameters>
			</Refresh>
		</Token>

		<Parameters>
			<Parameter Name="Authorization" Value="Bearer {{=token.Access}}" Type="HttpHeader" />
		</Parameters>
	</Authentication>

	<Resources>
		<Template>
			<Field Name="ShortText" DataType="DT_WSTR" Length="255" />
			<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="skip" Value="{{=parameters.iterators}}" />
						<Parameter Name="top" Value="{{=parameters.batchSize}}" />
						<Parameter Name="_includeUserParameters" Value="{{=parameters}}" />
					</Parameters>

					<Iterator>
						<Next Value="{{=parseInt(parameters.iterator || 0) + parameters.batchSize}}" />
					</Iterator>
				</Read>

				<Create Method="POST">
					<Parameters>
						<Parameter Name="_includeUserParameters" Value="{{=parameters}}" />
						<Parameter Name="application/json" Value="{{=item}}" Type="Body" />
					</Parameters>
				</Create>

				<Update Method="PUT">
					<Parameters>
						<Parameter Name="_includeUserParameters" Value="{{=parameters}}" />
						<Parameter Name="application/json" Value="{{=item}}" Type="Body" />
					</Parameters>
				</Update>

				<Delete Method="DELETE" />
			</Resource>
		</Template>
	</Resources>
	<Script>
		<Module Name="Main">
			<![CDATA[
require('underscore');
require('Uri');
require('base64');
]]>
		</Module>
	</Script>
</ConnectionManager>