From 25f2e05427bc4ca53c40bb0e6082d4dedeecee60 Mon Sep 17 00:00:00 2001 From: Matthieu Maitre Date: Sat, 26 Mar 2016 01:34:57 -0700 Subject: [PATCH] Definitions for ADAL.JS --- adal-angular/adal-angular-tests.ts | 46 +++++++++ adal-angular/adal-angular.d.ts | 40 ++++++++ adal-angular/adal-tests.ts | 15 +++ adal-angular/adal.d.ts | 146 +++++++++++++++++++++++++++++ 4 files changed, 247 insertions(+) create mode 100644 adal-angular/adal-angular-tests.ts create mode 100644 adal-angular/adal-angular.d.ts create mode 100644 adal-angular/adal-tests.ts create mode 100644 adal-angular/adal.d.ts diff --git a/adal-angular/adal-angular-tests.ts b/adal-angular/adal-angular-tests.ts new file mode 100644 index 0000000000..930d94388f --- /dev/null +++ b/adal-angular/adal-angular-tests.ts @@ -0,0 +1,46 @@ +/// + +// Code samples from: +// - https://github.com/AzureAD/azure-activedirectory-library-for-js +// - https://github.com/Azure-Samples/active-directory-angularjs-singlepageapp + +// Variable provided by AngularJS +var $httpProvider: angular.IHttpProvider = null; +var adalAuthenticationServiceProvider: adal.AdalAuthenticationServiceProvider = null; +var adalAuthenticationService: adal.AdalAuthenticationService = null; + +var endpoints = { + "https://yourhost/api": "b6a68585-5287-45b2-ba82-383ba1f60932", +}; +adalAuthenticationServiceProvider.init({ + tenant: "52d4b072-9470-49fb-8721-bc3a1c9912a1", + clientId: "e9a5a8b6-8af7-4719-9821-0deef255f68e", + endpoints: endpoints + }, + $httpProvider +); + +adalAuthenticationServiceProvider.init({ + clientId: "e9a5a8b6-8af7-4719-9821-0deef255f68e" + }, + $httpProvider +); + +adalAuthenticationServiceProvider.init( + { + clientId: 'cb68f72f...', + cacheLocation: 'localStorage' + }, + $httpProvider // pass http provider to inject request interceptor to attach tokens +); + +adalAuthenticationServiceProvider.init({ + tenant: 'Enter your tenant name here e.g. contoso.onmicrosoft.com', + clientId: 'Enter your client ID here e.g. e9a5a8b6-8af7-4719-9821-0deef255f68e', + extraQueryParameter: 'nux=1' + }, + $httpProvider +); + +adalAuthenticationService.login(); +adalAuthenticationService.logOut(); diff --git a/adal-angular/adal-angular.d.ts b/adal-angular/adal-angular.d.ts new file mode 100644 index 0000000000..13a8b52fb5 --- /dev/null +++ b/adal-angular/adal-angular.d.ts @@ -0,0 +1,40 @@ +// Type definitions for Active Directory Authentication Library (ADAL) for JavaScript 1.0.8 +// Project: https://github.com/AzureAD/azure-activedirectory-library-for-js +// Definitions by: mmaitre314 +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// +/// + +declare namespace adal { + + interface AdalAuthenticationServiceProvider { + init(configOptions: Config, httpProvider: angular.IHttpProvider): void; + } + + interface UserInfo { + isAuthenticated: boolean, + userName: string, + loginError: string, + profile: any + } + + interface AdalAuthenticationService { + + config: Config; + userInfo: UserInfo, + + login(): void; + loginInProgress(): boolean; + logOut(): void; + getCachedToken(resource: string): string; + acquireToken(resource: string): angular.IPromise; + getUser(): angular.IPromise; + getResourceForEndpoint(endpoint: string): string, + clearCache(): void; + clearCacheForResource(resource: string): void; + info(message: string): void; + verbose(message: string): void; + } + +} \ No newline at end of file diff --git a/adal-angular/adal-tests.ts b/adal-angular/adal-tests.ts new file mode 100644 index 0000000000..598134d0a0 --- /dev/null +++ b/adal-angular/adal-tests.ts @@ -0,0 +1,15 @@ +/// + +var endpoints = { + "https://yourhost/api": "b6a68585-5287-45b2-ba82-383ba1f60932", +}; + +var config : adal.Config = { + tenant: "52d4b072-9470-49fb-8721-bc3a1c9912a1", // Optional by default, it sends common + clientId: "e9a5a8b6-8af7-4719-9821-0deef255f68e", // Required + endpoints: endpoints // If you need to send CORS api requests. +}; + +var auth = new AuthenticationContext(config); + +var userName: string = auth.getCachedUser().userName; \ No newline at end of file diff --git a/adal-angular/adal.d.ts b/adal-angular/adal.d.ts new file mode 100644 index 0000000000..97cb741508 --- /dev/null +++ b/adal-angular/adal.d.ts @@ -0,0 +1,146 @@ +// Type definitions for Active Directory Authentication Library (ADAL) for JavaScript 1.0.8 +// Project: https://github.com/AzureAD/azure-activedirectory-library-for-js +// Definitions by: mmaitre314 +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare var AuthenticationContext: adal.AuthenticationContextStatic; + +declare module 'adal' { + export = AuthenticationContext; +} + +declare namespace adal { + + interface Config { + tenant?: string, + clientId: string, + redirectUri?: string, + cacheLocation?: string, + displayCall?: (urlNavigate: string) => any, + correlationId?: string, + loginResource?: string, + resource?: string + endpoints?: any // If you need to send CORS api requests. + extraQueryParameter?: string + } + + interface User { + userName: string, + profile: any + } + + interface RequestInfo { + valid: boolean, + parameters: any, + stateMatch: boolean, + stateResponse: string, + requestType: string + } + + interface AuthenticationContextStatic { + new (config: Config): AuthenticationContext; + } + + interface AuthenticationContext { + + instance: string; + config: Config; + + /** + * Gets initial Idtoken for the app backend + * Saves the resulting Idtoken in localStorage. + */ + login(): void; + loginInProgress(): boolean; + + /** + * Gets token for the specified resource from local storage cache + * @param {string} resource A URI that identifies the resource for which the token is valid. + * @returns {string} token if exists and not expired or null + */ + getCachedToken(resource: string): string; + + /** + * Retrieves and parse idToken from localstorage + * @returns {User} user object + */ + getCachedUser(): User; + + registerCallback(expectedState: string, resource: string, callback: (message: string, token: string) => any): void; + + /** + * Acquire token from cache if not expired and available. Acquires token from iframe if expired. + * @param {string} resource ResourceUri identifying the target resource + * @param {requestCallback} callback + */ + acquireToken(resource: string, callback: (message: string, token: string) => any): void; + + /** + * Redirect the Browser to Azure AD Authorization endpoint + * @param {string} urlNavigate The authorization request url + */ + promptUser(urlNavigate: string): void; + + /** + * Clear cache items. + */ + clearCache(): void; + + /** + * Clear cache items for a resource. + */ + clearCacheForResource(resource: string): void; + + /** + * Logout user will redirect page to logout endpoint. + * After logout, it will redirect to post_logout page if provided. + */ + logOut(): void; + + /** + * Gets a user profile + * @param {requestCallback} callback - The callback that handles the response. + */ + getUser(callback: (message: string, user?: User) => any): void; + + /** + * Checks if hash contains access token or id token or error_description + * @param {string} hash - Hash passed from redirect page + * @returns {Boolean} + */ + isCallback(hash: string): boolean; + + /** + * Gets login error + * @returns {string} error message related to login + */ + getLoginError(): string; + + /** + * Gets requestInfo from given hash. + * @returns {string} error message related to login + */ + getRequestInfo(hash: string): string; + + /** + * Saves token from hash that is received from redirect. + */ + saveTokenFromHash(requestInfo: RequestInfo): void; + + /** + * Gets resource for given endpoint if mapping is provided with config. + * @param {string} endpoint - API endpoint + * @returns {string} resource for this API endpoint + */ + getResourceForEndpoint(endpoint: string): string; + + handleWindowCallback(): void; + + log(level: number, message: string, error: any): void; + error(message: string, error: any): void; + warn(message: string): void; + info(message: string): void; + verbose(message: string): void; + } + +}