#2844 fix accessCode flow

This commit is contained in:
Anna Bodnia
2017-04-27 18:55:25 +03:00
parent 210b20eea8
commit d013e7a05c
7 changed files with 73 additions and 120 deletions

View File

@@ -31,7 +31,7 @@ export function logout(payload) {
}
}
export const preAuthorizeOauth2 = (payload) => ( { authActions, errActions } ) => {
export const preAuthorizeImplicit = (payload) => ( { authActions, errActions } ) => {
let { auth , token, isValid } = payload
let { schema, name } = auth
let flow = schema.get("flow")
@@ -70,35 +70,69 @@ export function authorizeOauth2(payload) {
export const authorizePassword = ( auth ) => ( { fn, authActions, errActions } ) => {
let { schema, name, username, password, passwordType, clientId, clientSecret } = auth
let credentials = {
let form = {
grant_type: "password",
scopes: encodeURIComponent(auth.scopes.join(scopeSeparator))
}
let req = {
url: schema.get("tokenUrl"),
method: "post",
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
query: {}
}
let query = {}
let headers = {}
if ( passwordType === "basic") {
req.headers.Authorization = "Basic " + btoa(username + ":" + password)
headers.Authorization = "Basic " + btoa(username + ":" + password)
} else {
credentials = Object.assign({}, credentials, {username} , {password})
Object.assign(form, {username}, {password})
if ( passwordType === "query") {
if ( clientId ) { req.query.client_id = clientId }
if ( clientSecret ) { req.query.client_secret = clientSecret }
if ( clientId ) { query.client_id = clientId }
if ( clientSecret ) { query.client_secret = clientSecret }
} else {
credentials = Object.assign({}, credentials, {client_id: clientId}, {client_secret: clientSecret})
Object.assign(form, {client_id: clientId}, {client_secret: clientSecret})
}
}
req.body = buildFormData(credentials)
return fn.fetch(req)
.then(( response ) => {
return authActions.authorizeRequest({ body: buildFormData(form), url: schema.get("tokenUrl"), name, headers, query, auth})
}
export const authorizeApplication = ( auth ) => ( { fn, authActions, errActions } ) => {
let { schema, scopes, name, clientId, clientSecret } = auth
let form = {
grant_type: "client_credentials",
client_id: clientId,
client_secret: clientSecret,
scope: scopes.join(scopeSeparator)
}
return authActions.authorizeRequest({body: buildFormData(form), name, url: schema.get("tokenUrl"), auth })
}
export const authorizeAccessCode = ( auth ) => ( { fn, authActions } ) => {
let { schema, name, clientId, clientSecret } = auth
let form = {
grant_type: "authorization_code",
code: auth.code,
client_id: clientId,
client_secret: clientSecret
}
return authActions.authorizeRequest({body: buildFormData(form), name, url: schema.get("tokenUrl"), auth})
}
export const authorizeRequest = ( data ) => ( { fn, authActions, errActions } ) => {
let { body, query={}, headers={}, name, url, auth } = data
let _headers = Object.assign({
"Accept":"application/json, text/plain, */*",
"Content-Type": "application/x-www-form-urlencoded"
}, headers)
fn.fetch({
url: url,
method: "post",
headers: _headers,
query: query,
body: body
})
.then(function (response) {
let token = JSON.parse(response.data)
let error = token && ( token.error || "" )
let parseError = token && ( token.parseError || "" )
@@ -123,56 +157,7 @@ export const authorizePassword = ( auth ) => ( { fn, authActions, errActions } )
return
}
authActions.authorizeOauth2({ auth, token })
authActions.authorizeOauth2({ auth, token})
})
.catch(err => { errActions.newAuthErr( err ) })
}
export const authorizeApplication = ( auth ) => ( { fn, authActions, errActions } ) => {
let { schema, scopes, name, clientId, clientSecret } = auth
let credentials = {
grant_type: "client_credentials",
client_id: clientId,
client_secret: clientSecret,
scope: scopes.join(scopeSeparator)
}
return fn.fetch({
url: schema.get("tokenUrl"),
method: "post",
headers: {
"Accept":"application/json, text/plain, */*",
"Content-Type": "application/x-www-form-urlencoded"
},
body: buildFormData(credentials)
})
.then(function (response) {
let token = JSON.parse(response.data)
let error = token && ( token.error || "" )
let parseError = token && ( token.parseError || "" )
if ( !response.ok ) {
errActions.newAuthErr( {
authId: name,
level: "error",
source: "auth",
message: response.statusText
} )
return
}
if ( error || parseError ) {
errActions.newAuthErr({
authId: name,
level: "error",
source: "auth",
message: JSON.stringify(token)
})
return
}
authActions.authorizeOauth2({ auth, token })
})
.catch(err => { errActions.newAuthErr( err ) })
}