|
| 1 | +import { Plugin, IonicNativePlugin, checkAvailability, InstanceProperty, CordovaInstance } from '@ionic-native/core'; |
| 2 | +import { Injectable } from '@angular/core'; |
| 3 | + |
| 4 | +export interface AuthenticationResult { |
| 5 | + |
| 6 | + accessToken: string; |
| 7 | + accesSTokenType: string; |
| 8 | + expiresOn: Date; |
| 9 | + idToken: string; |
| 10 | + isMultipleResourceRefreshToken: boolean; |
| 11 | + status: string; |
| 12 | + statusCode: number; |
| 13 | + tenantId: string; |
| 14 | + |
| 15 | + /** |
| 16 | + * Creates authorization header for web requests. |
| 17 | + * @returns {String} The authorization header. |
| 18 | + */ |
| 19 | + createAuthorizationHeader(): string; |
| 20 | + |
| 21 | +} |
| 22 | + |
| 23 | +export interface TokenCache { |
| 24 | + clear(): void; |
| 25 | + readItems(): Promise<TokenCacheItem[]>; |
| 26 | + deleteItem(item: TokenCacheItem): void; |
| 27 | +} |
| 28 | + |
| 29 | +export interface TokenCacheItem { |
| 30 | + accessToken: string; |
| 31 | + authority: string; |
| 32 | + clientId: string; |
| 33 | + displayableId: string; |
| 34 | + expiresOn: Date; |
| 35 | + isMultipleResourceRefreshToken: boolean; |
| 36 | + resource: string; |
| 37 | + tenantId: string; |
| 38 | + userInfo: UserInfo; |
| 39 | +} |
| 40 | + |
| 41 | +export interface UserInfo { |
| 42 | + displayableId: string; |
| 43 | + userId: string; |
| 44 | + familyName: string; |
| 45 | + givenName: string; |
| 46 | + identityProvider: string; |
| 47 | + passwordChangeUrl: string; |
| 48 | + passwordExpiresOn: Date; |
| 49 | + uniqueId: string; |
| 50 | +} |
| 51 | + |
| 52 | + |
| 53 | +/** |
| 54 | + * @name MS ADAL |
| 55 | + * @description |
| 56 | + * Active Directory Authentication Library (ADAL) plugin. |
| 57 | + * Active Directory Authentication Library ([ADAL](https://docs.microsoft.com/en-us/dotnet/api/microsoft.identitymodel.clients.activedirectory?view=azure-dotnet)) |
| 58 | + * plugin provides easy to use authentication functionality for your Apache Cordova apps by taking advantage of |
| 59 | + * Windows Server Active Directory and Windows Azure Active Directory. Here you can find the source code for the library. |
| 60 | + * @usage |
| 61 | + * ```typescript |
| 62 | + * import { MSAdal, AuthenticationContext, AuthenticationResult } from '@ionic-native/ms-adal'; |
| 63 | + * |
| 64 | + * |
| 65 | + * constructor(private msAdal: MSAdal) {} |
| 66 | + * |
| 67 | + * ... |
| 68 | + * |
| 69 | + * let authContext: AuthenticationContext = this.msAdal.createAuthenticationContext('https://login.windows.net/common'); |
| 70 | + * |
| 71 | + * authContext.acquireTokenAsync('https://graph.windows.net', 'a5d92493-ae5a-4a9f-bcbf-9f1d354067d3', 'http://MyDirectorySearcherApp') |
| 72 | + * .then((authResponse: AuthenticationResult) => { |
| 73 | + * console.log('Token is' , authResponse.accessToken); |
| 74 | + * console.log('Token will expire on', authResponse.expiresOn); |
| 75 | + * }) |
| 76 | + * .catch((e: any) => console.log('Authentication failed', e)); |
| 77 | + * |
| 78 | + * |
| 79 | + * ``` |
| 80 | + * |
| 81 | + * @classes |
| 82 | + * AuthenticationContext |
| 83 | + * @interfaces |
| 84 | + * AuthenticationResult |
| 85 | + * TokenCache |
| 86 | + * TokenCacheItem |
| 87 | + * UserInfo |
| 88 | + */ |
| 89 | +@Plugin({ |
| 90 | + pluginName: 'MSADAL', |
| 91 | + plugin: 'cordova-plugin-ms-adal', |
| 92 | + pluginRef: 'Microsoft.ADAL', |
| 93 | + repo: 'https://github.com/AzureAD/azure-activedirectory-library-for-cordova', |
| 94 | + platforms: ['Android', 'iOS', 'Windows'] |
| 95 | +}) |
| 96 | +@Injectable() |
| 97 | +export class MSAdal extends IonicNativePlugin { |
| 98 | + |
| 99 | + createAuthenticationContext(authority: string, validateAuthority: boolean = true) { |
| 100 | + let authContext: any; |
| 101 | + if (checkAvailability(MSAdal.getPluginRef(), null, MSAdal.getPluginName()) === true) { |
| 102 | + authContext = new (MSAdal.getPlugin()).AuthenticationContext(authority); |
| 103 | + } |
| 104 | + return new AuthenticationContext(authContext); |
| 105 | + } |
| 106 | + |
| 107 | +} |
| 108 | + |
| 109 | +export class AuthenticationContext { |
| 110 | + |
| 111 | + @InstanceProperty |
| 112 | + authority: string; |
| 113 | + |
| 114 | + @InstanceProperty |
| 115 | + validateAuthority: boolean; |
| 116 | + |
| 117 | + @InstanceProperty |
| 118 | + tokenCache: any; |
| 119 | + |
| 120 | + constructor(private _objectInstance: any) {} |
| 121 | + |
| 122 | + /** |
| 123 | + * Acquires token using interactive flow. It always shows UI and skips token from cache. |
| 124 | + * |
| 125 | + * @param {String} resourceUrl Resource identifier |
| 126 | + * @param {String} clientId Client (application) identifier |
| 127 | + * @param {String} redirectUrl Redirect url for this application |
| 128 | + * @param {String} userId User identifier (optional) |
| 129 | + * @param {String} extraQueryParameters |
| 130 | + * Extra query parameters (optional) |
| 131 | + * Parameters should be escaped before passing to this method (e.g. using 'encodeURI()') |
| 132 | + * @returns {Promise} Promise either fulfilled with AuthenticationResult object or rejected with error |
| 133 | + */ |
| 134 | + @CordovaInstance({ |
| 135 | + otherPromise: true |
| 136 | + }) |
| 137 | + acquireTokenAsync(resourceUrl: string, clientId: string, redirectUrl: string, userId: string, extraQueryParameters?: any): Promise<AuthenticationResult> { return; } |
| 138 | + |
| 139 | + /** |
| 140 | + * Acquires token WITHOUT using interactive flow. It checks the cache to return existing result |
| 141 | + * if not expired. It tries to use refresh token if available. If it fails to get token without |
| 142 | + * displaying UI it will fail. This method guarantees that no UI will be shown to user. |
| 143 | + * |
| 144 | + * @param {String} resourceUrl Resource identifier |
| 145 | + * @param {String} clientId Client (application) identifier |
| 146 | + * @param {String} userId User identifier (optional) |
| 147 | + * @returns {Promise} Promise either fulfilled with AuthenticationResult object or rejected with error |
| 148 | + */ |
| 149 | + @CordovaInstance({ |
| 150 | + otherPromise: true |
| 151 | + }) |
| 152 | + acquireTokenSilentAsync(resourceUrl: string, clientId: string, userId: string): Promise<AuthenticationResult> { return; } |
| 153 | + |
| 154 | +} |
0 commit comments