Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom authority url param added to authorize #211

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@ export default class Auth {
* @param {String} parameters.scope a space-separated list of scopes that you want the user to consent to.
* @param {String} parameters.prompt (optional) indicates the type of user interaction that is required.
* The only valid values at this time are 'login', 'none', and 'consent'.
* @param {String} parameters.authorityUrl (optional)the authorityUrl for signup or other flows directly
* @returns {String} authorize url with specified parameters to redirect to for AuthZ/AuthN.
*
* @see https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-v2-protocols-oauth-code
*
* @memberof Auth
*/
loginUrl(parameters = {}) {
const {authorityUrl, ...restParameters} = parameters;
const query = validate({
parameters: {
responseType: { required: true, toName: 'response_type' },
Expand All @@ -69,12 +71,12 @@ export default class Auth {
prompt: {}
},
validate: false // not declared params are allowed:
}, parameters)
}, restParameters)
return this.client.url('authorize',
{...query,
client_id: this.clientId,
redirect_uri: this.redirectUri
})
}, authorityUrl)
}

/**
Expand All @@ -99,27 +101,29 @@ export default class Auth {
* @param {String} input.code code returned by `/authorize`.
* @param {String} input.redirectUri original redirectUri used when calling `/authorize`.
* @param {String} input.scope A space-separated list of scopes.
* @param {String} input.authorityUrl A space-separated list of scopes.
* The scopes requested in this leg must be equivalent to or a subset of the scopes requested in the first leg
* @returns {Promise}
* @see https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-v2-protocols-oauth-code#request-an-access-token
*
* @memberof Auth
*/
exchange(input = {}) {
const {authorityUrl, ...restInput} = input;
const payload = validate({
parameters: {
code: { required: true },
scope: { required: true },
code_verifier: { required: true },
}
}, input)
}, restInput)

return this.client
.post('token',
{...payload,
client_id: this.clientId,
redirect_uri: this.redirectUri,
grant_type: 'authorization_code'})
grant_type: 'authorization_code'}, authorityUrl)
.then(responseHandler)
}

Expand Down
9 changes: 5 additions & 4 deletions src/networking/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export default class Client {
}
}

post(path, body) {
let url = this.url(path)
post(path, body, authorityUrl) {
let url = this.url(path, null, authorityUrl)
return this.request('POST', url, body)
}

Expand All @@ -51,8 +51,9 @@ export default class Client {
return this.request('GET', this.url(path, query))
}

url(path, query) {
let endpoint = url.resolve(this.baseUrl, path)
url(path, query, authorityUrl) {
const baseAuthorityUrl = authorityUrl ? `https://${authorityUrl}`: this.baseUrl;
let endpoint = url.resolve(baseAuthorityUrl, path)
if (query && query.length !== 0) {
const parsed = url.parse(endpoint)
parsed.query = query || {}
Expand Down
4 changes: 3 additions & 1 deletion src/webauth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export default class WebAuth {
* The only valid values are 'login', 'none', 'consent', and 'select_account'.
* @see https://learn.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow
* @param {Boolean} [options.ephemeralSession] SSO. It only affects iOS with versions 13 and above.
* @param {String} options.authorityUrl (optional)the authorityUrl for signup or other flows directly
* @returns {Promise<BaseTokenItem | AccessTokenItem>}
*
* @memberof WebAuth
Expand Down Expand Up @@ -98,7 +99,8 @@ export default class WebAuth {
const tokenResponse = await client.exchange({
code,
scope: scope.toString(),
code_verifier: verifier
code_verifier: verifier,
authorityUrl: options?.authorityUrl
})

if (tokenResponse.refreshToken) {
Expand Down
7 changes: 5 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ declare class Client {
bearer: string;
get(path: string, query: string): Promise<void>;
patch(path: string, body: any): Promise<void>;
post(path: string, body: any): Promise<void>;
post(path: string, body: any, authorityUrl?: string): Promise<void>;
/**
* Helper function to send HTTP requests
*
Expand All @@ -75,7 +75,7 @@ declare class Client {
* @param {Object} [body] - request body
*/
request(method: string, url: string, body?: any): Promise<ClientResponse>;
url(path: string, query?: string): string;
url(path: string, query?: string, authorityUrl?: string): string;
}

/**
Expand Down Expand Up @@ -224,6 +224,7 @@ declare class Auth {
* @param {String} input.code code returned by `/authorize`.
* @param {String} input.redirectUri original redirectUri used when calling `/authorize`.
* @param {String} input.scope A space-separated list of scopes.
* @param {String} input.authorityUrl A space-separated list of scopes.
* The scopes requested in this leg must be equivalent to or a subset of the scopes requested in the first leg
*
* @see https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-v2-protocols-oauth-code#request-an-access-token
Expand All @@ -232,6 +233,7 @@ declare class Auth {
code: string;
redirectUri: string;
scope: string;
authorityUrl?: string;
}): Promise<void>;
/**
* Builds the full authorize endpoint url in the Authorization Server (AS) with given parameters.
Expand Down Expand Up @@ -307,6 +309,7 @@ declare class WebAuth {
authorize(options: {
prompt?: string;
scope?: string;
authorityUrl?: string;
}): Promise<BaseTokenItem & Partial<AccessTokenItem>>;
/**
* Removes Azure session
Expand Down