-
Notifications
You must be signed in to change notification settings - Fork 9.2k
Added support for '{realm}' placeholder in authorizationUrl and tokenUrl #3410
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
base: master
Are you sure you want to change the base?
Changes from all commits
8e143bf
3525003
79c7ab7
f1a710d
2fc3b50
bb94db0
30122e9
7fa5543
701e867
f71044d
64a2467
898331b
d9ec8ab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -113,7 +113,7 @@ Config Name | Description | |
--- | --- | ||
client_id | Default clientId. MUST be a string | ||
client_secret | Default clientSecret. MUST be a string | ||
realm | realm query parameter (for oauth1) added to `authorizationUrl` and `tokenUrl` . MUST be a string | ||
realm | The OAuth realm parameter for `authorizationUrl` and `tokenUrl`. Optional. If specified it MUST be a string. If a `{realm}` placeholder is present in the URL path, it will be replaced with this value. If no such placeholder is present, the value will be added as the value for querystring parameter 'realm'. | ||
appName | application name, displayed in authorization popup. MUST be a string | ||
scopeSeparator | scope separator for passing scopes, encoded before calling, default value is a space (encoded value `%20`). MUST be a string | ||
additionalQueryStringParams | Additional query parameters added to `authorizationUrl` and `tokenUrl`. MUST be an object | ||
|
@@ -126,7 +126,7 @@ const ui = SwaggerUIBundle({...}) | |
ui.initOAuth({ | ||
clientId: "your-client-id", | ||
clientSecret: "your-client-secret-if-required", | ||
realm: "your-realms", | ||
realm: "your-realm", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Afaict from all the info I've read on |
||
appName: "your-app-name", | ||
scopeSeparator: " ", | ||
additionalQueryStringParams: {test: "hello"} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -109,15 +109,14 @@ export default class Auths extends React.Component { | |
<p>API requires the following scopes. Select which ones you want to grant to Swagger UI.</p> | ||
</div> | ||
{ | ||
definitions.filter( schema => schema.get("type") === "oauth2") | ||
.map( (schema, name) =>{ | ||
oauthDefinitions.map( (schema, name) =>{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return (<div key={ name }> | ||
<Oauth2 authorized={ authorized } | ||
schema={ schema } | ||
name={ name } /> | ||
</div>) | ||
} | ||
).toArray() | ||
).toArray() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some whitespace conversion by my editor probably, sorry. |
||
} | ||
</div> : null | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import React from "react" | ||
import PropTypes from "prop-types" | ||
import oauth2Authorize from "core/oauth2-authorize" | ||
import oauth2Authorize, { processUrl } from "core/oauth2-authorize" | ||
|
||
const IMPLICIT = "implicit" | ||
const ACCESS_CODE = "accessCode" | ||
|
@@ -27,6 +27,7 @@ export default class Oauth2 extends React.Component { | |
let authConfigs = authSelectors.getConfigs() || {} | ||
let username = auth && auth.get("username") || "" | ||
let clientId = auth && auth.get("clientId") || authConfigs.clientId || "" | ||
let realm = auth && auth.get("realm") || authConfigs.realm || "" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make sure we get the realm setting as well |
||
let clientSecret = auth && auth.get("clientSecret") || authConfigs.clientSecret || "" | ||
let passwordType = auth && auth.get("passwordType") || "request-body" | ||
|
||
|
@@ -39,7 +40,8 @@ export default class Oauth2 extends React.Component { | |
clientSecret: clientSecret, | ||
username: username, | ||
password: "", | ||
passwordType: passwordType | ||
passwordType: passwordType, | ||
realm: realm | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add it to the state so we can access it later |
||
} | ||
|
||
|
@@ -108,8 +110,8 @@ export default class Oauth2 extends React.Component { | |
|
||
{ isAuthorized && <h6>Authorized</h6> } | ||
|
||
{ ( flow === IMPLICIT || flow === ACCESS_CODE ) && <p>Authorization URL: <code>{ schema.get("authorizationUrl") }</code></p> } | ||
{ ( flow === PASSWORD || flow === ACCESS_CODE || flow === APPLICATION ) && <p>Token URL:<code> { schema.get("tokenUrl") }</code></p> } | ||
{ ( flow === IMPLICIT || flow === ACCESS_CODE ) && <p>Authorization URL: <code>{ processUrl(schema.get("authorizationUrl"), this.state) }</code></p> } | ||
{ ( flow === PASSWORD || flow === ACCESS_CODE || flow === APPLICATION ) && <p>Token URL:<code> { processUrl(schema.get("tokenUrl"), this.state) }</code></p> } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Call |
||
<p className="flow">Flow: <code>{ schema.get("flow") }</code></p> | ||
|
||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,19 +52,7 @@ export default function authorize ( { auth, authActions, errActions, configs, au | |
|
||
query.push("state=" + encodeURIComponent(state)) | ||
|
||
if (typeof authConfigs.realm !== "undefined") { | ||
query.push("realm=" + encodeURIComponent(authConfigs.realm)) | ||
} | ||
|
||
let { additionalQueryStringParams } = authConfigs | ||
|
||
for (let key in additionalQueryStringParams) { | ||
if (typeof additionalQueryStringParams[key] !== "undefined") { | ||
query.push([key, additionalQueryStringParams[key]].map(encodeURIComponent).join("=")) | ||
} | ||
} | ||
|
||
let url = [schema.get("authorizationUrl"), query.join("&")].join("?") | ||
const url = processUrl(schema.get("authorizationUrl"), authConfigs, query) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The removed code above is now part of |
||
|
||
// pass action authorizeOauth2 and authentication data through window | ||
// to authorize with oauth2 | ||
|
@@ -88,3 +76,30 @@ export default function authorize ( { auth, authActions, errActions, configs, au | |
|
||
win.open(url) | ||
} | ||
|
||
function processUrl(url, authConfigs, query=[]) { | ||
let result = url || "" | ||
if (authConfigs) { | ||
if (authConfigs.realm) { | ||
const placeholder = "{realm}" | ||
const idx = url ? url.indexOf(placeholder) : -1 | ||
if (idx !== -1) { | ||
result = url.substring(0, idx) + encodeURIComponent(authConfigs.realm) + url.substring(idx + placeholder.length) | ||
} else { | ||
query.push("realm=" + encodeURIComponent(authConfigs.realm)) | ||
} | ||
} | ||
const params = authConfigs.additionalQueryStringParams || {} | ||
for (let key in params) { | ||
if (params[key] !== undefined) { | ||
query.push([key, params[key]].map(encodeURIComponent).join("=")) | ||
} | ||
} | ||
if (query.length) { | ||
result += (result.indexOf("?") === -1 ? "?" : "&") + query.join("&") | ||
} | ||
} | ||
return result | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the meat of it. The existing code to fill in the configs and additional query params is now in this function, where I also added the bit that does the replacement of the placeholder. |
||
|
||
export { processUrl } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import win from "core/window" | ||
import { btoa, buildFormData } from "core/utils" | ||
import { processUrl } from "core/oauth2-authorize" | ||
|
||
export const SHOW_AUTH_POPUP = "show_popup" | ||
export const AUTHORIZE = "authorize" | ||
|
@@ -141,12 +142,8 @@ export const authorizeAccessCodeWithBasicAuthentication = ( { auth, redirectUrl | |
|
||
export const authorizeRequest = ( data ) => ( { fn, authActions, errActions, authSelectors } ) => { | ||
let { body, query={}, headers={}, name, url, auth } = data | ||
let { additionalQueryStringParams } = authSelectors.getConfigs() || {} | ||
let fetchUrl = url | ||
|
||
for (let key in additionalQueryStringParams) { | ||
url += "&" + key + "=" + encodeURIComponent(additionalQueryStringParams[key]) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is now done by |
||
let authConfigs = authSelectors.getConfigs() || {} | ||
let fetchUrl = processUrl(url, authConfigs) | ||
|
||
let _headers = Object.assign({ | ||
"Accept":"application/json, text/plain, */*", | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The old docs were very concise. I added some more detail and mentioned the
{realm}
placeholder andrealm
query parameter explicitly so people (hopefully) understand what it does.