Bladeren bron

Implement OAuth2 password flow

bubble
TANAKA Koichi 8 jaren geleden
bovenliggende
commit
7cdf83a932
5 gewijzigde bestanden met toevoegingen van 60 en 7 verwijderingen
  1. +28
    -1
      src/main/javascript/view/AuthView.js
  2. +4
    -1
      src/main/javascript/view/Oauth2Model.js
  3. +11
    -1
      src/main/javascript/view/Oauth2View.js
  4. +5
    -0
      src/main/less/auth.less
  5. +12
    -4
      src/main/template/oauth2.handlebars

+ 28
- 1
src/main/javascript/view/AuthView.js Bestand weergeven

@@ -122,6 +122,12 @@ SwaggerUi.Views.AuthView = Backbone.View.extend({
this.clientCredentialsFlow(scopes, dets.tokenUrl, container.OAuthSchemeKey);
return;
}
else if(auth.get('type') === 'oauth2' && flow && (flow === 'password')) {
dets = auth.attributes;
window.swaggerUi.tokenName = dets.tokenName || 'access_token';
this.passwordFlow(scopes, dets.tokenUrl, dets.username, dets.password, window.OAuthSchemeKey);
return;
}
else if(auth.get('grantTypes')) {
// 1.2 support
var o = auth.get('grantTypes');
@@ -176,6 +182,27 @@ SwaggerUi.Views.AuthView = Backbone.View.extend({
onOAuthComplete('');
}
});
}
},

passwordFlow: function (scopes, tokenUrl, username, password, OAuthSchemeKey) {
var params = {
'scope': scopes.join(' '),
'username': username,
'password': password,
'grant_type': 'password'
};
$.ajax({
url : tokenUrl,
type: 'POST',
data: params,
success: function (data)
{
onOAuthComplete(data, OAuthSchemeKey);
},
error: function ()
{
onOAuthComplete('');
}
});
}
});

+ 4
- 1
src/main/javascript/view/Oauth2Model.js Bestand weergeven

@@ -2,7 +2,8 @@

SwaggerUi.Models.Oauth2Model = Backbone.Model.extend({
defaults: {
scopes: {}
scopes: {},
isPasswordFlow: false
},

initialize: function () {
@@ -19,6 +20,8 @@ SwaggerUi.Models.Oauth2Model = Backbone.Model.extend({
attributes.scopes = scopes;
this.attributes = attributes;
}

this.set('isPasswordFlow', attributes.flow && attributes.flow === 'password');
this.on('change', this.validate);
},



+ 11
- 1
src/main/javascript/view/Oauth2View.js Bestand weergeven

@@ -2,7 +2,9 @@

SwaggerUi.Views.Oauth2View = Backbone.View.extend({
events: {
'change .oauth-scope': 'scopeChange'
'change .oauth-scope': 'scopeChange',
'change .oauth-username': 'setUsername',
'change .oauth-password': 'setPassword'
},

template: Handlebars.templates.oauth2,
@@ -18,5 +20,13 @@ SwaggerUi.Views.Oauth2View = Backbone.View.extend({
var scope = $(e.target).data('scope');

this.model.setScopes(scope, val);
},

setUsername: function (e) {
this.model.set('username', $(e.target).val());
},

setPassword: function (e) {
this.model.set('password', $(e.target).val());
}
});

+ 5
- 0
src/main/less/auth.less Bestand weergeven

@@ -201,4 +201,9 @@
}

.api-popup-actions { padding-top: 10px; }

fieldset {
padding-bottom: 10px;
padding-left: 20px;
}
}

+ 12
- 4
src/main/template/oauth2.handlebars Bestand weergeven

@@ -1,12 +1,20 @@
<div>
<h3 class="auth__title">Select OAuth2.0 Scopes</h3>
<h3 class="auth__title">OAuth2.0</h3>
<p>{{{sanitize description}}}</p>
{{#if authorizationUrl}}<p>Authorization URL: {{{sanitize authorizationUrl}}}</p>{{/if}}
{{#if tokenUrl}}<p>Token URL: {{{sanitize tokenUrl}}}</p>{{/if}}
<p>flow: {{{escape flow}}}</p>
{{#if isPasswordFlow}}
<p>Please input username and password for password flow authorization</p>
<fieldset>
<div><label>Username: <input class="oauth-username" type="text" name="username"></label></div>
<div><label>Password: <input class="oauth-password" type="password" name="password"></label></div>
</fieldset>
{{/if}}
<p><strong> {{{escape appName}}} </strong> API requires the following scopes. Select which ones you want to grant to Swagger UI.</p>
<p>Scopes are used to grant an application different levels of access to data on behalf of the end user. Each API may declare one or more scopes.
<a href="#">Learn how to use</a>
</p>
<p><strong> {{{escape appName}}} </strong> API requires the following scopes. Select which ones you want to grant to Swagger UI.</p>
<p>Authorization URL: {{{sanitize authorizationUrl}}}</p>
<p>flow: {{{escape flow}}}</p>
<ul class="api-popup-scopes">
{{#each scopes}}
<li>


Laden…
Annuleren
Opslaan