From 0cb722c27a180d8f14f6713ef78279968989da68 Mon Sep 17 00:00:00 2001 From: Kai Walter Date: Sun, 13 Mar 2016 19:26:53 +0100 Subject: [PATCH 1/5] initial cordova-plugin-ms-adal.d.ts --- .../cordova-plugin-ms-adal-tests.ts | 47 ++++ .../cordova-plugin-ms-adal.d.ts | 214 ++++++++++++++++++ 2 files changed, 261 insertions(+) create mode 100644 cordova-plugin-ms-adal/cordova-plugin-ms-adal-tests.ts create mode 100644 cordova-plugin-ms-adal/cordova-plugin-ms-adal.d.ts diff --git a/cordova-plugin-ms-adal/cordova-plugin-ms-adal-tests.ts b/cordova-plugin-ms-adal/cordova-plugin-ms-adal-tests.ts new file mode 100644 index 0000000000..d45e12262f --- /dev/null +++ b/cordova-plugin-ms-adal/cordova-plugin-ms-adal-tests.ts @@ -0,0 +1,47 @@ +/// + +function test_cordova_plugin_ms_adal() { + + var testConfiguration = { + authority: 'https://login.microsoftonline.com/testtenant.onmicrosoft.com', + resourceUrl: 'https://graph.windows.net/', + clientId: '12345678-1234-1234-1234-123456789abc', + redirectUrl: 'http://app.testtenant.onmicrosoft.com' + }; + + // create AuthenticationContext + Microsoft.ADAL.AuthenticationContext.createAsync(testConfiguration.authority) + .then((context) => { + + context.tokenCache.readItems().then((cacheItems) => { + if (cacheItems.length >= 1) { + var testUserId; + testUserId = cacheItems[0].userInfo.userId; + + context.acquireTokenSilentAsync(testConfiguration.resourceUrl, testConfiguration.clientId, testUserId).then((authResult) => { + console.log("Silently acquired token successfully"); + }, (err) => { + console.log("Failed to acquire token silently " + JSON.stringify(err)); + }); + } + else { + + context.acquireTokenAsync(testConfiguration.resourceUrl, testConfiguration.clientId, testConfiguration.redirectUrl) + .then((authResult) => { + console.log("Acquired token successfully"); + }, (err) => { + console.log("Failed to acquire token " + JSON.stringify(err)); + }); + } + }); + + // clear TokenCache + context.tokenCache.clear(); + + }, (message) => { + console.log(message); + }); + + +} + diff --git a/cordova-plugin-ms-adal/cordova-plugin-ms-adal.d.ts b/cordova-plugin-ms-adal/cordova-plugin-ms-adal.d.ts new file mode 100644 index 0000000000..2e4685243e --- /dev/null +++ b/cordova-plugin-ms-adal/cordova-plugin-ms-adal.d.ts @@ -0,0 +1,214 @@ +// Type definitions for Active Directory Authentication Library (ADAL) plugin for Apache Cordova +// Project: https://github.com/AzureAD/azure-activedirectory-library-for-cordova +// Definitions by: Kai Walter +// Definitions: https://github.com/KaiWalter/DefinitelyTyped + +declare namespace Microsoft { + + namespace ADAL { + + interface IUserInfo { + displayableId: string, + userId: string, + familyName: string, + givenName: string, + identityProvider: string, + passwordChangeUrl: string, + passwordExpiresOn: Date, + uniqueId: string, + } + + interface ITokenCache { + contextAuthority: string + } + + interface ITokenCacheItem { + accessToken: string, + authority: string, + clientId: string, + displayableId: string, + expiresOn: Date, + isMultipleResourceRefreshToken: boolean, + resource: string, + tenantId: string, + userInfo: IUserInfo + } + + interface IPromise { + then(doneCallBack: () => void, failCallBack?: (message: string) => void); + } + + interface IPromiseTokenCacheItems { + then(doneCallBack: (tokenCacheItems: ITokenCacheItem[]) => void, failCallBack?: (message: string) => void); + } + + class TokenCache implements ITokenCache { + contextAuthority: string + + /** + * Clears the cache by deleting all the items. + * + * @returns {Promise} Promise either fulfilled when operation is completed or rejected with error. + */ + clear(): IPromise; + + /** + * Gets all cached items. + * + * @returns {Promise} Promise either fulfilled with array of cached items or rejected with error. + */ + readItems(): IPromiseTokenCacheItems; + + /** + * Deletes cached item. + * + * @param {TokenCacheItem} item Cached item to delete from cache + * + * @returns {Promise} Promise either fulfilled when operation is completed or rejected with error. + */ + deleteItem(item: ITokenCacheItem): IPromise; + } + + interface IAuthenticationResult { + accessToken: string, + accessTokenType: string, + expiresOn: Date, + idToken: string, + isMultipleResourceRefreshToken: boolean, + status: string, + statusCode: string, + tenantId: string, + userInfo: IUserInfo + } + + class AuthenticationResult implements IAuthenticationResult { + accessToken: string; + accessTokenType: string; + expiresOn: Date; + idToken: string; + isMultipleResourceRefreshToken: boolean; + status: string; + statusCode: string; + tenantId: string; + userInfo: IUserInfo; + + /** + * Creates authorization header for web requests. + * + * @returns {String} The authorization header. + */ + createAuthorizationHeader(): string; + } + + interface IPromiseAuthenticationResult { + then(doneCallBack: (context: IAuthenticationResult) => void, failCallBack?: (message: string) => void); + } + + interface IAuthenticationContext { + authority: string, + validateAuthority: boolean, + tokenCache: TokenCache + + /** + * Acquires token using interactive flow if needed. It checks the cache to return existing result + * if not expired. It tries to use refresh token if available. If it fails to get token with + * refresh token, it will remove this refresh token from cache and start authentication. + * + * @param {String} resourceUrl Resource identifier + * @param {String} clientId Client (application) identifier + * @param {String} redirectUrl Redirect url for this application + * @param {String} userId User identifier (optional) + * @param {String} extraQueryParameters + * Extra query parameters (optional) + * Parameters should be escaped before passing to this method (e.g. using 'encodeURI()') + * + * @returns {Promise} Promise either fulfilled with AuthenticationResult object or rejected with error + */ + acquireTokenAsync(resourceUrl: string, clientId: string, redirectUrl: string, userId?: string, extraQueryParameters?: string): IPromiseAuthenticationResult; + + /** + * Acquires token WITHOUT using interactive flow. It checks the cache to return existing result + * if not expired. It tries to use refresh token if available. If it fails to get token without + * displaying UI it will fail. This method guarantees that no UI will be shown to user. + * + * @param {String} resourceUrl Resource identifier + * @param {String} clientId Client (application) identifier + * @param {String} userId User identifier (optional) + * + * @returns {Promise} Promise either fulfilled with AuthenticationResult object or rejected with error + */ + acquireTokenSilentAsync(resourceUrl: string, clientId: string, userId: string): IPromiseAuthenticationResult; + + } + + interface IPromiseAuthenticationContext { + then(doneCallBack: (context: IAuthenticationContext) => void, failCallBack?: (message: string) => void); + } + + class AuthenticationContext implements IAuthenticationContext { + authority: string; + validateAuthority: boolean; + tokenCache: TokenCache; + + /** + * Constructs context to use with known authority to get the token. It reuses existing context + * for this authority URL in native proxy or creates a new one if it doesn't exists. + * Corresponding native context will be created at first time when it will be needed. + * + * @param {String} authority Authority url to send code and token requests + * @param {Boolean} validateAuthority Validate authority before sending token request + * When context is being created syncronously using this constructor + * validateAuthority in native context will be disabled to prevent + * context initialization failure + * + * @returns {Object} Newly created authentication context. + */ + constructor(authority: string, validateAuthority?: boolean); + + /** + * Constructs context asynchronously to use with known authority to get the token. + * It reuses existing context for this authority URL in native proxy or creates a new one if it doesn't exists. + * + * @param {String} authority Authority url to send code and token requests + * @param {Boolean} validateAuthority Validate authority before sending token request. True by default + * + * @returns {Promise} Promise either fulfilled with newly created authentication context or rejected with error + */ + static createAsync(authority: string, validateAuthority?: boolean): IPromiseAuthenticationContext; + + /** + * Acquires token using interactive flow if needed. It checks the cache to return existing result + * if not expired. It tries to use refresh token if available. If it fails to get token with + * refresh token, it will remove this refresh token from cache and start authentication. + * + * @param {String} resourceUrl Resource identifier + * @param {String} clientId Client (application) identifier + * @param {String} redirectUrl Redirect url for this application + * @param {String} userId User identifier (optional) + * @param {String} extraQueryParameters + * Extra query parameters (optional) + * Parameters should be escaped before passing to this method (e.g. using 'encodeURI()') + * + * @returns {Promise} Promise either fulfilled with AuthenticationResult object or rejected with error + */ + acquireTokenAsync(resourceUrl: string, clientId: string, redirectUrl: string, userId?: string, extraQueryParameters?: string): IPromiseAuthenticationResult; + + /** + * Acquires token WITHOUT using interactive flow. It checks the cache to return existing result + * if not expired. It tries to use refresh token if available. If it fails to get token without + * displaying UI it will fail. This method guarantees that no UI will be shown to user. + * + * @param {String} resourceUrl Resource identifier + * @param {String} clientId Client (application) identifier + * @param {String} userId User identifier (optional) + * + * @returns {Promise} Promise either fulfilled with AuthenticationResult object or rejected with error + */ + acquireTokenSilentAsync(resourceUrl: string, clientId: string, userId: string): IPromiseAuthenticationResult; + + } + } +} + + + From a446d802e4c7251073730ba678e6605440f3a3b9 Mon Sep 17 00:00:00 2001 From: Kai Walter Date: Sun, 13 Mar 2016 19:53:52 +0100 Subject: [PATCH 2/5] initial cordova-plugin-ms-adal.d.ts + fixed TS7010 --- cordova-plugin-ms-adal/cordova-plugin-ms-adal-tests.ts | 2 +- cordova-plugin-ms-adal/cordova-plugin-ms-adal.d.ts | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cordova-plugin-ms-adal/cordova-plugin-ms-adal-tests.ts b/cordova-plugin-ms-adal/cordova-plugin-ms-adal-tests.ts index d45e12262f..8bdb7f1427 100644 --- a/cordova-plugin-ms-adal/cordova-plugin-ms-adal-tests.ts +++ b/cordova-plugin-ms-adal/cordova-plugin-ms-adal-tests.ts @@ -15,7 +15,7 @@ function test_cordova_plugin_ms_adal() { context.tokenCache.readItems().then((cacheItems) => { if (cacheItems.length >= 1) { - var testUserId; + var testUserId: string; testUserId = cacheItems[0].userInfo.userId; context.acquireTokenSilentAsync(testConfiguration.resourceUrl, testConfiguration.clientId, testUserId).then((authResult) => { diff --git a/cordova-plugin-ms-adal/cordova-plugin-ms-adal.d.ts b/cordova-plugin-ms-adal/cordova-plugin-ms-adal.d.ts index 2e4685243e..68e88abcde 100644 --- a/cordova-plugin-ms-adal/cordova-plugin-ms-adal.d.ts +++ b/cordova-plugin-ms-adal/cordova-plugin-ms-adal.d.ts @@ -35,11 +35,11 @@ declare namespace Microsoft { } interface IPromise { - then(doneCallBack: () => void, failCallBack?: (message: string) => void); + then(doneCallBack: () => any, failCallBack?: (message: string) => any):any; } interface IPromiseTokenCacheItems { - then(doneCallBack: (tokenCacheItems: ITokenCacheItem[]) => void, failCallBack?: (message: string) => void); + then(doneCallBack: (tokenCacheItems: ITokenCacheItem[]) => any, failCallBack?: (message: string) => any):any; } class TokenCache implements ITokenCache { @@ -101,7 +101,7 @@ declare namespace Microsoft { } interface IPromiseAuthenticationResult { - then(doneCallBack: (context: IAuthenticationResult) => void, failCallBack?: (message: string) => void); + then(doneCallBack: (context: IAuthenticationResult) => any, failCallBack?: (message: string) => any):any; } interface IAuthenticationContext { @@ -142,7 +142,7 @@ declare namespace Microsoft { } interface IPromiseAuthenticationContext { - then(doneCallBack: (context: IAuthenticationContext) => void, failCallBack?: (message: string) => void); + then(doneCallBack: (context: IAuthenticationContext) => any, failCallBack?: (message: string) => any):any; } class AuthenticationContext implements IAuthenticationContext { From 01b2d882bd78afc527fad22de9be66aa2781c7a2 Mon Sep 17 00:00:00 2001 From: Kai Walter Date: Sun, 13 Mar 2016 20:41:45 +0100 Subject: [PATCH 3/5] fixed header --- cordova-plugin-ms-adal/cordova-plugin-ms-adal.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cordova-plugin-ms-adal/cordova-plugin-ms-adal.d.ts b/cordova-plugin-ms-adal/cordova-plugin-ms-adal.d.ts index 68e88abcde..0d77511e03 100644 --- a/cordova-plugin-ms-adal/cordova-plugin-ms-adal.d.ts +++ b/cordova-plugin-ms-adal/cordova-plugin-ms-adal.d.ts @@ -1,4 +1,4 @@ -// Type definitions for Active Directory Authentication Library (ADAL) plugin for Apache Cordova +// Type definitions for Active Directory Authentication Library plugin for Apache Cordova // Project: https://github.com/AzureAD/azure-activedirectory-library-for-cordova // Definitions by: Kai Walter // Definitions: https://github.com/KaiWalter/DefinitelyTyped From 7f6cceb3e5b5c61a1ca531bf20655e9fec0d8df0 Mon Sep 17 00:00:00 2001 From: Kai Walter Date: Tue, 15 Mar 2016 18:56:38 +0100 Subject: [PATCH 4/5] renamed Interfaces to conventions --- .../cordova-plugin-ms-adal.d.ts | 93 ++++--------------- 1 file changed, 20 insertions(+), 73 deletions(-) diff --git a/cordova-plugin-ms-adal/cordova-plugin-ms-adal.d.ts b/cordova-plugin-ms-adal/cordova-plugin-ms-adal.d.ts index 0d77511e03..10ebdda79f 100644 --- a/cordova-plugin-ms-adal/cordova-plugin-ms-adal.d.ts +++ b/cordova-plugin-ms-adal/cordova-plugin-ms-adal.d.ts @@ -7,7 +7,7 @@ declare namespace Microsoft { namespace ADAL { - interface IUserInfo { + interface UserInfo { displayableId: string, userId: string, familyName: string, @@ -18,11 +18,7 @@ declare namespace Microsoft { uniqueId: string, } - interface ITokenCache { - contextAuthority: string - } - - interface ITokenCacheItem { + interface TokenCacheItem { accessToken: string, authority: string, clientId: string, @@ -31,18 +27,18 @@ declare namespace Microsoft { isMultipleResourceRefreshToken: boolean, resource: string, tenantId: string, - userInfo: IUserInfo + userInfo: UserInfo } - interface IPromise { + interface Promise { then(doneCallBack: () => any, failCallBack?: (message: string) => any):any; } - interface IPromiseTokenCacheItems { - then(doneCallBack: (tokenCacheItems: ITokenCacheItem[]) => any, failCallBack?: (message: string) => any):any; + interface PromiseTokenCacheItems { + then(doneCallBack: (tokenCacheItems: TokenCacheItem[]) => any, failCallBack?: (message: string) => any):any; } - class TokenCache implements ITokenCache { + class TokenCache { contextAuthority: string /** @@ -50,14 +46,14 @@ declare namespace Microsoft { * * @returns {Promise} Promise either fulfilled when operation is completed or rejected with error. */ - clear(): IPromise; + clear(): Promise; /** * Gets all cached items. * * @returns {Promise} Promise either fulfilled with array of cached items or rejected with error. */ - readItems(): IPromiseTokenCacheItems; + readItems(): PromiseTokenCacheItems; /** * Deletes cached item. @@ -66,22 +62,10 @@ declare namespace Microsoft { * * @returns {Promise} Promise either fulfilled when operation is completed or rejected with error. */ - deleteItem(item: ITokenCacheItem): IPromise; + deleteItem(item: TokenCacheItem): Promise; } - interface IAuthenticationResult { - accessToken: string, - accessTokenType: string, - expiresOn: Date, - idToken: string, - isMultipleResourceRefreshToken: boolean, - status: string, - statusCode: string, - tenantId: string, - userInfo: IUserInfo - } - - class AuthenticationResult implements IAuthenticationResult { + class AuthenticationResult { accessToken: string; accessTokenType: string; expiresOn: Date; @@ -90,7 +74,7 @@ declare namespace Microsoft { status: string; statusCode: string; tenantId: string; - userInfo: IUserInfo; + userInfo: UserInfo; /** * Creates authorization header for web requests. @@ -100,52 +84,15 @@ declare namespace Microsoft { createAuthorizationHeader(): string; } - interface IPromiseAuthenticationResult { - then(doneCallBack: (context: IAuthenticationResult) => any, failCallBack?: (message: string) => any):any; + interface PromiseAuthenticationResult { + then(doneCallBack: (context: AuthenticationResult) => any, failCallBack?: (message: string) => any):any; } - interface IAuthenticationContext { - authority: string, - validateAuthority: boolean, - tokenCache: TokenCache - - /** - * Acquires token using interactive flow if needed. It checks the cache to return existing result - * if not expired. It tries to use refresh token if available. If it fails to get token with - * refresh token, it will remove this refresh token from cache and start authentication. - * - * @param {String} resourceUrl Resource identifier - * @param {String} clientId Client (application) identifier - * @param {String} redirectUrl Redirect url for this application - * @param {String} userId User identifier (optional) - * @param {String} extraQueryParameters - * Extra query parameters (optional) - * Parameters should be escaped before passing to this method (e.g. using 'encodeURI()') - * - * @returns {Promise} Promise either fulfilled with AuthenticationResult object or rejected with error - */ - acquireTokenAsync(resourceUrl: string, clientId: string, redirectUrl: string, userId?: string, extraQueryParameters?: string): IPromiseAuthenticationResult; - - /** - * Acquires token WITHOUT using interactive flow. It checks the cache to return existing result - * if not expired. It tries to use refresh token if available. If it fails to get token without - * displaying UI it will fail. This method guarantees that no UI will be shown to user. - * - * @param {String} resourceUrl Resource identifier - * @param {String} clientId Client (application) identifier - * @param {String} userId User identifier (optional) - * - * @returns {Promise} Promise either fulfilled with AuthenticationResult object or rejected with error - */ - acquireTokenSilentAsync(resourceUrl: string, clientId: string, userId: string): IPromiseAuthenticationResult; - + interface PromiseAuthenticationContext { + then(doneCallBack: (context: AuthenticationContext) => any, failCallBack?: (message: string) => any):any; } - interface IPromiseAuthenticationContext { - then(doneCallBack: (context: IAuthenticationContext) => any, failCallBack?: (message: string) => any):any; - } - - class AuthenticationContext implements IAuthenticationContext { + class AuthenticationContext implements AuthenticationContext { authority: string; validateAuthority: boolean; tokenCache: TokenCache; @@ -174,7 +121,7 @@ declare namespace Microsoft { * * @returns {Promise} Promise either fulfilled with newly created authentication context or rejected with error */ - static createAsync(authority: string, validateAuthority?: boolean): IPromiseAuthenticationContext; + static createAsync(authority: string, validateAuthority?: boolean): PromiseAuthenticationContext; /** * Acquires token using interactive flow if needed. It checks the cache to return existing result @@ -191,7 +138,7 @@ declare namespace Microsoft { * * @returns {Promise} Promise either fulfilled with AuthenticationResult object or rejected with error */ - acquireTokenAsync(resourceUrl: string, clientId: string, redirectUrl: string, userId?: string, extraQueryParameters?: string): IPromiseAuthenticationResult; + acquireTokenAsync(resourceUrl: string, clientId: string, redirectUrl: string, userId?: string, extraQueryParameters?: string): PromiseAuthenticationResult; /** * Acquires token WITHOUT using interactive flow. It checks the cache to return existing result @@ -204,7 +151,7 @@ declare namespace Microsoft { * * @returns {Promise} Promise either fulfilled with AuthenticationResult object or rejected with error */ - acquireTokenSilentAsync(resourceUrl: string, clientId: string, userId: string): IPromiseAuthenticationResult; + acquireTokenSilentAsync(resourceUrl: string, clientId: string, userId: string): PromiseAuthenticationResult; } } From 492a10db5c1bf8bab6447d6bd71aa282bd2c3665 Mon Sep 17 00:00:00 2001 From: Kai Walter Date: Wed, 16 Mar 2016 19:12:52 +0100 Subject: [PATCH 5/5] fixed AuthenticationContext class --- cordova-plugin-ms-adal/cordova-plugin-ms-adal.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cordova-plugin-ms-adal/cordova-plugin-ms-adal.d.ts b/cordova-plugin-ms-adal/cordova-plugin-ms-adal.d.ts index 10ebdda79f..407de6347a 100644 --- a/cordova-plugin-ms-adal/cordova-plugin-ms-adal.d.ts +++ b/cordova-plugin-ms-adal/cordova-plugin-ms-adal.d.ts @@ -92,7 +92,7 @@ declare namespace Microsoft { then(doneCallBack: (context: AuthenticationContext) => any, failCallBack?: (message: string) => any):any; } - class AuthenticationContext implements AuthenticationContext { + class AuthenticationContext { authority: string; validateAuthority: boolean; tokenCache: TokenCache;