Merge pull request #3084 from tcaesvk/feature/oauth2-authorize-query

fix OAuth2 authorize issue + remove redundant parameters
This commit is contained in:
Anna
2017-05-16 15:18:20 +03:00
committed by GitHub

View File

@@ -3,26 +3,35 @@ import { btoa } from "core/utils"
export default function authorize ( { auth, authActions, errActions, configs, authConfigs={} } ) { export default function authorize ( { auth, authActions, errActions, configs, authConfigs={} } ) {
let { schema, scopes, name, clientId } = auth let { schema, scopes, name, clientId } = auth
let { additionalQueryStringParams } = authConfigs
let redirectUrl = configs.oauth2RedirectUrl
let scopeSeparator = authConfigs.scopeSeparator || " "
let state = btoa(new Date())
let flow = schema.get("flow") let flow = schema.get("flow")
let url let query = []
if (flow === "password") { switch (flow) {
authActions.authorizePassword(auth) case "password":
return authActions.authorizePassword(auth)
return
case "application":
authActions.authorizeApplication(auth)
return
case "accessCode":
query.push("response_type=code")
break
case "implicit":
query.push("response_type=token")
break
} }
if (flow === "application") { if (typeof clientId === "string") {
authActions.authorizeApplication(auth) query.push("client_id=" + encodeURIComponent(clientId))
return
} }
let redirectUrl = configs.oauth2RedirectUrl
// todo move to parser // todo move to parser
if ( !redirectUrl ) { if (typeof redirectUrl === "undefined") {
errActions.newAuthErr( { errActions.newAuthErr( {
authId: name, authId: name,
source: "validation", source: "validation",
@@ -31,21 +40,32 @@ export default function authorize ( { auth, authActions, errActions, configs, au
}) })
return return
} }
query.push("redirect_uri=" + encodeURIComponent(redirectUrl))
if (flow === "implicit" || flow === "accessCode") { if (Array.isArray(scopes) && 0 < scopes.length) {
url = schema.get("authorizationUrl") + "?response_type=" + (flow === "implicit" ? "token" : "code") let scopeSeparator = authConfigs.scopeSeparator || " "
query.push("scope=" + encodeURIComponent(scopes.join(scopeSeparator)))
} }
url += "&redirect_uri=" + encodeURIComponent(redirectUrl) let state = btoa(new Date())
+ "&realm=" + encodeURIComponent(authConfigs.realm);
+ "&scope=" + encodeURIComponent(scopes.join(scopeSeparator)) query.push("state=" + encodeURIComponent(state))
+ "&state=" + encodeURIComponent(state)
+ "&client_id=" + encodeURIComponent(clientId) if (typeof authConfigs.realm !== "undefined") {
query.push("realm=" + encodeURIComponent(authConfigs.realm))
}
let { additionalQueryStringParams } = authConfigs
for (let key in additionalQueryStringParams) { for (let key in additionalQueryStringParams) {
url += "&" + key + "=" + encodeURIComponent(additionalQueryStringParams[key]) if (typeof additionalQueryStringParams[key] !== "undefined") {
query.push([key, additionalQueryStringParams[key]].map(encodeURIComponent).join("="))
}
} }
let url = [schema.get("authorizationUrl"), query.join("&")].join("?")
// pass action authorizeOauth2 and authentication data through window // pass action authorizeOauth2 and authentication data through window
// to authorize with oauth2 // to authorize with oauth2
win.swaggerUIRedirectOauth2 = { win.swaggerUIRedirectOauth2 = {