Skip to content

Commit 94d8792

Browse files
committed
Merge pull request DefinitelyTyped#8533 from KaiWalter/master
new definition for cordova-plugin-ms-adal
2 parents f55baad + 492a10d commit 94d8792

File tree

2 files changed

+208
-0
lines changed

2 files changed

+208
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/// <reference path="./cordova-plugin-ms-adal.d.ts"/>
2+
3+
function test_cordova_plugin_ms_adal() {
4+
5+
var testConfiguration = {
6+
authority: 'https://login.microsoftonline.com/testtenant.onmicrosoft.com',
7+
resourceUrl: 'https://graph.windows.net/',
8+
clientId: '12345678-1234-1234-1234-123456789abc',
9+
redirectUrl: 'http://app.testtenant.onmicrosoft.com'
10+
};
11+
12+
// create AuthenticationContext
13+
Microsoft.ADAL.AuthenticationContext.createAsync(testConfiguration.authority)
14+
.then((context) => {
15+
16+
context.tokenCache.readItems().then((cacheItems) => {
17+
if (cacheItems.length >= 1) {
18+
var testUserId: string;
19+
testUserId = cacheItems[0].userInfo.userId;
20+
21+
context.acquireTokenSilentAsync(testConfiguration.resourceUrl, testConfiguration.clientId, testUserId).then((authResult) => {
22+
console.log("Silently acquired token successfully");
23+
}, (err) => {
24+
console.log("Failed to acquire token silently " + JSON.stringify(err));
25+
});
26+
}
27+
else {
28+
29+
context.acquireTokenAsync(testConfiguration.resourceUrl, testConfiguration.clientId, testConfiguration.redirectUrl)
30+
.then((authResult) => {
31+
console.log("Acquired token successfully");
32+
}, (err) => {
33+
console.log("Failed to acquire token " + JSON.stringify(err));
34+
});
35+
}
36+
});
37+
38+
// clear TokenCache
39+
context.tokenCache.clear();
40+
41+
}, (message) => {
42+
console.log(message);
43+
});
44+
45+
46+
}
47+
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
// Type definitions for Active Directory Authentication Library plugin for Apache Cordova
2+
// Project: https://github.com/AzureAD/azure-activedirectory-library-for-cordova
3+
// Definitions by: Kai Walter <https://github.com/KaiWalter>
4+
// Definitions: https://github.com/KaiWalter/DefinitelyTyped
5+
6+
declare namespace Microsoft {
7+
8+
namespace ADAL {
9+
10+
interface UserInfo {
11+
displayableId: string,
12+
userId: string,
13+
familyName: string,
14+
givenName: string,
15+
identityProvider: string,
16+
passwordChangeUrl: string,
17+
passwordExpiresOn: Date,
18+
uniqueId: string,
19+
}
20+
21+
interface TokenCacheItem {
22+
accessToken: string,
23+
authority: string,
24+
clientId: string,
25+
displayableId: string,
26+
expiresOn: Date,
27+
isMultipleResourceRefreshToken: boolean,
28+
resource: string,
29+
tenantId: string,
30+
userInfo: UserInfo
31+
}
32+
33+
interface Promise {
34+
then(doneCallBack: () => any, failCallBack?: (message: string) => any):any;
35+
}
36+
37+
interface PromiseTokenCacheItems {
38+
then(doneCallBack: (tokenCacheItems: TokenCacheItem[]) => any, failCallBack?: (message: string) => any):any;
39+
}
40+
41+
class TokenCache {
42+
contextAuthority: string
43+
44+
/**
45+
* Clears the cache by deleting all the items.
46+
*
47+
* @returns {Promise} Promise either fulfilled when operation is completed or rejected with error.
48+
*/
49+
clear(): Promise;
50+
51+
/**
52+
* Gets all cached items.
53+
*
54+
* @returns {Promise} Promise either fulfilled with array of cached items or rejected with error.
55+
*/
56+
readItems(): PromiseTokenCacheItems;
57+
58+
/**
59+
* Deletes cached item.
60+
*
61+
* @param {TokenCacheItem} item Cached item to delete from cache
62+
*
63+
* @returns {Promise} Promise either fulfilled when operation is completed or rejected with error.
64+
*/
65+
deleteItem(item: TokenCacheItem): Promise;
66+
}
67+
68+
class AuthenticationResult {
69+
accessToken: string;
70+
accessTokenType: string;
71+
expiresOn: Date;
72+
idToken: string;
73+
isMultipleResourceRefreshToken: boolean;
74+
status: string;
75+
statusCode: string;
76+
tenantId: string;
77+
userInfo: UserInfo;
78+
79+
/**
80+
* Creates authorization header for web requests.
81+
*
82+
* @returns {String} The authorization header.
83+
*/
84+
createAuthorizationHeader(): string;
85+
}
86+
87+
interface PromiseAuthenticationResult {
88+
then(doneCallBack: (context: AuthenticationResult) => any, failCallBack?: (message: string) => any):any;
89+
}
90+
91+
interface PromiseAuthenticationContext {
92+
then(doneCallBack: (context: AuthenticationContext) => any, failCallBack?: (message: string) => any):any;
93+
}
94+
95+
class AuthenticationContext {
96+
authority: string;
97+
validateAuthority: boolean;
98+
tokenCache: TokenCache;
99+
100+
/**
101+
* Constructs context to use with known authority to get the token. It reuses existing context
102+
* for this authority URL in native proxy or creates a new one if it doesn't exists.
103+
* Corresponding native context will be created at first time when it will be needed.
104+
*
105+
* @param {String} authority Authority url to send code and token requests
106+
* @param {Boolean} validateAuthority Validate authority before sending token request
107+
* When context is being created syncronously using this constructor
108+
* validateAuthority in native context will be disabled to prevent
109+
* context initialization failure
110+
*
111+
* @returns {Object} Newly created authentication context.
112+
*/
113+
constructor(authority: string, validateAuthority?: boolean);
114+
115+
/**
116+
* Constructs context asynchronously to use with known authority to get the token.
117+
* It reuses existing context for this authority URL in native proxy or creates a new one if it doesn't exists.
118+
*
119+
* @param {String} authority Authority url to send code and token requests
120+
* @param {Boolean} validateAuthority Validate authority before sending token request. True by default
121+
*
122+
* @returns {Promise} Promise either fulfilled with newly created authentication context or rejected with error
123+
*/
124+
static createAsync(authority: string, validateAuthority?: boolean): PromiseAuthenticationContext;
125+
126+
/**
127+
* Acquires token using interactive flow if needed. It checks the cache to return existing result
128+
* if not expired. It tries to use refresh token if available. If it fails to get token with
129+
* refresh token, it will remove this refresh token from cache and start authentication.
130+
*
131+
* @param {String} resourceUrl Resource identifier
132+
* @param {String} clientId Client (application) identifier
133+
* @param {String} redirectUrl Redirect url for this application
134+
* @param {String} userId User identifier (optional)
135+
* @param {String} extraQueryParameters
136+
* Extra query parameters (optional)
137+
* Parameters should be escaped before passing to this method (e.g. using 'encodeURI()')
138+
*
139+
* @returns {Promise} Promise either fulfilled with AuthenticationResult object or rejected with error
140+
*/
141+
acquireTokenAsync(resourceUrl: string, clientId: string, redirectUrl: string, userId?: string, extraQueryParameters?: string): PromiseAuthenticationResult;
142+
143+
/**
144+
* Acquires token WITHOUT using interactive flow. It checks the cache to return existing result
145+
* if not expired. It tries to use refresh token if available. If it fails to get token without
146+
* displaying UI it will fail. This method guarantees that no UI will be shown to user.
147+
*
148+
* @param {String} resourceUrl Resource identifier
149+
* @param {String} clientId Client (application) identifier
150+
* @param {String} userId User identifier (optional)
151+
*
152+
* @returns {Promise} Promise either fulfilled with AuthenticationResult object or rejected with error
153+
*/
154+
acquireTokenSilentAsync(resourceUrl: string, clientId: string, userId: string): PromiseAuthenticationResult;
155+
156+
}
157+
}
158+
}
159+
160+
161+

0 commit comments

Comments
 (0)