Add audience and resource field for OAuth2 Authorization Code grant (#1768)

* add audience to grant authorization code

* add resource to grant authorization code

* fixed audience and resource for code grant
This commit is contained in:
Duncan Brown
2019-11-22 12:48:34 -05:00
committed by Gregory Schier
parent 2834d839d0
commit 83be4a9714
4 changed files with 51 additions and 3 deletions

View File

@@ -14,6 +14,8 @@ const CLIENT_SECRET = 'secret_12345456677756343';
const REDIRECT_URI = 'https://foo.com/redirect';
const SCOPE = 'scope_123';
const STATE = 'state_123';
const AUDIENCE = 'https://foo.com/resource';
const RESOURCE = 'foo.com';
describe('authorization_code', () => {
beforeEach(globalBeforeEach);
@@ -27,6 +29,8 @@ describe('authorization_code', () => {
access_token: 'token_123',
token_type: 'token_type',
scope: SCOPE,
audience: AUDIENCE,
resource: RESOURCE,
}),
);
@@ -48,6 +52,8 @@ describe('authorization_code', () => {
REDIRECT_URI,
SCOPE,
STATE,
AUDIENCE,
RESOURCE,
);
// Check the request to fetch the token
@@ -64,6 +70,8 @@ describe('authorization_code', () => {
{ name: 'code', value: 'code_123' },
{ name: 'redirect_uri', value: REDIRECT_URI },
{ name: 'state', value: STATE },
{ name: 'audience', value: AUDIENCE },
{ name: 'resource', value: RESOURCE },
],
},
headers: [
@@ -91,6 +99,8 @@ describe('authorization_code', () => {
expires_in: null,
token_type: 'token_type',
scope: SCOPE,
audience: AUDIENCE,
resource: RESOURCE,
error: null,
error_uri: null,
error_description: null,
@@ -108,6 +118,8 @@ describe('authorization_code', () => {
access_token: 'token_123',
token_type: 'token_type',
scope: SCOPE,
audience: AUDIENCE,
resource: RESOURCE,
}),
);
@@ -129,6 +141,8 @@ describe('authorization_code', () => {
REDIRECT_URI,
SCOPE,
STATE,
AUDIENCE,
RESOURCE,
);
// Check the request to fetch the token
@@ -145,6 +159,8 @@ describe('authorization_code', () => {
{ name: 'code', value: 'code_123' },
{ name: 'redirect_uri', value: REDIRECT_URI },
{ name: 'state', value: STATE },
{ name: 'audience', value: AUDIENCE },
{ name: 'resource', value: RESOURCE },
{ name: 'client_id', value: CLIENT_ID },
{ name: 'client_secret', value: CLIENT_SECRET },
],
@@ -170,6 +186,8 @@ describe('authorization_code', () => {
expires_in: null,
token_type: 'token_type',
scope: SCOPE,
audience: AUDIENCE,
resource: RESOURCE,
error: null,
error_uri: null,
error_description: null,

View File

@@ -63,6 +63,8 @@ async function _getOAuth2AuthorizationCodeHeader(
authentication.redirectUrl,
authentication.scope,
authentication.state,
authentication.audience,
authentication.resource,
);
return _updateOAuth2Token(requestId, results);

View File

@@ -18,6 +18,8 @@ export default async function(
redirectUri: string = '',
scope: string = '',
state: string = '',
audience: string = '',
resource: string = '',
): Promise<Object> {
if (!authorizeUrl) {
throw new Error('Invalid authorization URL');
@@ -27,7 +29,15 @@ export default async function(
throw new Error('Invalid access token URL');
}
const authorizeResults = await _authorize(authorizeUrl, clientId, redirectUri, scope, state);
const authorizeResults = await _authorize(
authorizeUrl,
clientId,
redirectUri,
scope,
state,
audience,
resource,
);
// Handle the error
if (authorizeResults[c.P_ERROR]) {
@@ -46,10 +56,20 @@ export default async function(
authorizeResults[c.P_CODE],
redirectUri,
state,
audience,
resource,
);
}
async function _authorize(url, clientId, redirectUri = '', scope = '', state = '') {
async function _authorize(
url,
clientId,
redirectUri = '',
scope = '',
state = '',
audience = '',
resource = '',
) {
const params = [
{ name: c.P_RESPONSE_TYPE, value: c.RESPONSE_TYPE_CODE },
{ name: c.P_CLIENT_ID, value: clientId },
@@ -59,6 +79,8 @@ async function _authorize(url, clientId, redirectUri = '', scope = '', state = '
redirectUri && params.push({ name: c.P_REDIRECT_URI, value: redirectUri });
scope && params.push({ name: c.P_SCOPE, value: scope });
state && params.push({ name: c.P_STATE, value: state });
audience && params.push({ name: c.P_AUDIENCE, value: audience });
resource && params.push({ name: c.P_RESOURCE, value: resource });
// Add query params to URL
const qs = buildQueryStringFromParams(params);
@@ -89,6 +111,8 @@ async function _getToken(
code: string,
redirectUri: string = '',
state: string = '',
audience: string = '',
resource: string = '',
): Promise<Object> {
const params = [
{ name: c.P_GRANT_TYPE, value: c.GRANT_TYPE_AUTHORIZATION_CODE },
@@ -98,6 +122,8 @@ async function _getToken(
// Add optional params
redirectUri && params.push({ name: c.P_REDIRECT_URI, value: redirectUri });
state && params.push({ name: c.P_STATE, value: state });
audience && params.push({ name: c.P_AUDIENCE, value: audience });
resource && params.push({ name: c.P_RESOURCE, value: resource });
const headers = [
{ name: 'Content-Type', value: 'application/x-www-form-urlencoded' },
@@ -145,6 +171,8 @@ async function _getToken(
c.P_EXPIRES_IN,
c.P_TOKEN_TYPE,
c.P_SCOPE,
c.P_AUDIENCE,
c.P_RESOURCE,
c.P_ERROR,
c.P_ERROR_URI,
c.P_ERROR_DESCRIPTION,

View File

@@ -425,7 +425,7 @@ class OAuth2Auth extends React.PureComponent<Props, State> {
enabled,
];
advancedFields = [scope, state, credentialsInBody, tokenPrefix];
advancedFields = [scope, state, credentialsInBody, tokenPrefix, audience, resource];
} else if (grantType === GRANT_TYPE_CLIENT_CREDENTIALS) {
basicFields = [accessTokenUrl, clientId, clientSecret, enabled];