Merge pull request #2864 from misi/master

Implement Application/Client Credentials  Flow
This commit is contained in:
Anna
2017-04-06 15:50:39 +03:00
committed by GitHub
3 changed files with 40 additions and 3 deletions

View File

@@ -145,7 +145,7 @@ export default class Oauth2 extends React.Component {
}
{
( flow === IMPLICIT || flow === ACCESS_CODE || ( flow === PASSWORD && this.state.passwordType!== "none") ) &&
( flow === APPLICATION || flow === IMPLICIT || flow === ACCESS_CODE || ( flow === PASSWORD && this.state.passwordType!== "none") ) &&
( !isAuthorized || isAuthorized && this.state.clientId) && <Row>
<label htmlFor="client_id">client_id:</label>
<Col tablet={10} desktop={10}>
@@ -159,7 +159,7 @@ export default class Oauth2 extends React.Component {
}
{
( flow === ACCESS_CODE || ( flow === PASSWORD && this.state.passwordType!== "none") ) && <Row>
( flow === APPLICATION || flow === ACCESS_CODE || ( flow === PASSWORD && this.state.passwordType!== "none") ) && <Row>
<label htmlFor="client_secret">client_secret:</label>
<Col tablet={10} desktop={10}>
{
@@ -205,7 +205,7 @@ export default class Oauth2 extends React.Component {
} )
}
<div className="auth-btn-wrapper">
{ isValid && flow !== APPLICATION &&
{ isValid &&
( isAuthorized ? <Button className="btn modal-btn auth authorize" onClick={ this.logout }>Logout</Button>
: <Button className="btn modal-btn auth authorize" onClick={ this.authorize }>Authorize</Button>
)

View File

@@ -14,6 +14,11 @@ export default function authorize ( auth, authActions, errActions, configs ) {
return
}
if (flow === "application") {
authActions.authorizeOauth2Application(auth)
return
}
// todo move to parser
if ( !redirectUrl ) {
errActions.newAuthErr( {

View File

@@ -119,3 +119,35 @@ export const authorizePassword = ( auth ) => ( { fn, authActions, errActions } )
})
.catch(err => { errActions.newAuthErr( err ) })
}
export const authorizeOauth2Application = ( auth ) => ( { fn, authActions, errActions } ) => {
let { schema, scopes, name, clientId, clientSecret } = auth
fn.fetch(schema.get("tokenUrl"), {
method: "post", headers: {
"Accept":"application/json, text/plain, */*",
"Content-Type": "application/x-www-form-urlencoded"
},
body: "grant_type=client_credentials" +
"&client_id=" + encodeURIComponent(clientId) +
"&client_secret=" + encodeURIComponent(clientSecret) +
"&scope=" + encodeURIComponent(scopes.join(scopeSeparator))
})
.then(function (response) {
if ( !response.ok ) {
errActions.newAuthErr( {
authId: name,
level: "error",
source: "auth",
message: response.statusText
} )
return
} else {
response.json()
.then(function (json){
authActions.authorizeOauth2({ auth, token: json})
})
}
})
.catch(err => { errActions.newAuthErr( err ) })
}