Allow optional specification of Origin header for Auth Code flow + PKCE (#3783)

Co-authored-by: Opender Singh <opender.singh@konghq.com>
This commit is contained in:
Paul Johnson 2021-08-09 23:30:32 +01:00 committed by GitHub
parent 8478fbc27f
commit 366a54f630
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 1 deletions

View File

@ -71,6 +71,7 @@ async function _getOAuth2AuthorizationCodeHeader(
authentication.resource, authentication.resource,
authentication.usePkce, authentication.usePkce,
authentication.pkceMethod, authentication.pkceMethod,
authentication.origin,
); );
return _updateOAuth2Token(requestId, results); return _updateOAuth2Token(requestId, results);
} }
@ -190,6 +191,7 @@ async function _getAccessToken(
authentication.clientSecret, authentication.clientSecret,
token.refreshToken, token.refreshToken,
authentication.scope, authentication.scope,
authentication.origin,
); );
// If we didn't receive an access token it means the refresh token didn't succeed, // If we didn't receive an access token it means the refresh token didn't succeed,

View File

@ -23,6 +23,7 @@ export default async function(
resource = '', resource = '',
usePkce = false, usePkce = false,
pkceMethod = c.PKCE_CHALLENGE_S256, pkceMethod = c.PKCE_CHALLENGE_S256,
origin = '',
): Promise<Record<string, any>> { ): Promise<Record<string, any>> {
if (!authorizeUrl) { if (!authorizeUrl) {
throw new Error('Invalid authorization URL'); throw new Error('Invalid authorization URL');
@ -80,6 +81,7 @@ export default async function(
audience, audience,
resource, resource,
codeVerifier, codeVerifier,
origin,
); );
} }
@ -171,6 +173,7 @@ async function _getToken(
audience = '', audience = '',
resource = '', resource = '',
codeVerifier = '', codeVerifier = '',
origin = '',
): Promise<Record<string, any>> { ): Promise<Record<string, any>> {
const params = [ const params = [
{ {
@ -232,6 +235,10 @@ async function _getToken(
headers.push(getBasicAuthHeader(clientId, clientSecret)); headers.push(getBasicAuthHeader(clientId, clientSecret));
} }
if (origin) {
headers.push({ name: 'Origin', value: origin });
}
const responsePatch = await sendWithSettings(requestId, { const responsePatch = await sendWithSettings(requestId, {
headers, headers,
url, url,

View File

@ -14,6 +14,7 @@ export default async function(
clientSecret: string, clientSecret: string,
refreshToken: string, refreshToken: string,
scope: string, scope: string,
origin: string,
): Promise<Record<string, any>> { ): Promise<Record<string, any>> {
const params = [ const params = [
{ {
@ -55,6 +56,10 @@ export default async function(
headers.push(getBasicAuthHeader(clientId, clientSecret)); headers.push(getBasicAuthHeader(clientId, clientSecret));
} }
if (origin) {
headers.push({ name: 'Origin', value: origin });
}
const url = setDefaultProtocol(accessTokenUrl); const url = setDefaultProtocol(accessTokenUrl);
const response = await sendWithSettings(requestId, { const response = await sendWithSettings(requestId, {
headers, headers,

View File

@ -241,6 +241,10 @@ class OAuth2Auth extends PureComponent<Props, State> {
this._handleChangeProperty('resource', value); this._handleChangeProperty('resource', value);
} }
_handleChangeOrigin(value: string) {
this._handleChangeProperty('origin', value);
}
_handleChangeGrantType(e: React.SyntheticEvent<HTMLInputElement>) { _handleChangeGrantType(e: React.SyntheticEvent<HTMLInputElement>) {
this._handleChangeProperty('grantType', e.currentTarget.value); this._handleChangeProperty('grantType', e.currentTarget.value);
} }
@ -497,6 +501,12 @@ class OAuth2Auth extends PureComponent<Props, State> {
this._handleChangeResource, this._handleChangeResource,
'Indicate what resource to access', 'Indicate what resource to access',
); );
const origin = this.renderInputRow(
'Origin',
'origin',
this._handleChangeOrigin,
'Specify Origin header when CORS is required for oauth endpoints',
);
const credentialsInBody = this.renderSelectRow( const credentialsInBody = this.renderSelectRow(
'Credentials', 'Credentials',
'credentialsInBody', 'credentialsInBody',
@ -527,7 +537,7 @@ class OAuth2Auth extends PureComponent<Props, State> {
enabled, enabled,
]; ];
advancedFields = [scope, state, credentialsInBody, tokenPrefix, audience, resource]; advancedFields = [scope, state, credentialsInBody, tokenPrefix, audience, resource, origin];
} else if (grantType === GRANT_TYPE_CLIENT_CREDENTIALS) { } else if (grantType === GRANT_TYPE_CLIENT_CREDENTIALS) {
basicFields = [accessTokenUrl, clientId, clientSecret, enabled]; basicFields = [accessTokenUrl, clientId, clientSecret, enabled];
advancedFields = [scope, credentialsInBody, tokenPrefix, audience, resource]; advancedFields = [scope, credentialsInBody, tokenPrefix, audience, resource];