Skip to content

Commit

Permalink
oauth2 password bearer grant
Browse files Browse the repository at this point in the history
  • Loading branch information
timini committed Mar 23, 2016
1 parent 0c6c83c commit af71679
Show file tree
Hide file tree
Showing 27 changed files with 200 additions and 14 deletions.
6 changes: 6 additions & 0 deletions client/app/authenticators/oauth2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import OAuth2PasswordGrant from 'ember-simple-auth/authenticators/oauth2-password-grant';

export default OAuth2PasswordGrant.extend({
serverTokenEndpoint: '/oauth/token/',
clientId: 'nzN8xQBxEwqjHtR7Kq6220mvIVY6QHRGxUNY8UWa',
});
3 changes: 3 additions & 0 deletions client/app/authorizers/oauth2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import OAuth2Bearer from 'ember-simple-auth/authorizers/oauth2-bearer';

export default OAuth2Bearer.extend();
10 changes: 10 additions & 0 deletions client/app/controllers/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Ember from 'ember';

export default Ember.Controller.extend({
session: Ember.inject.service('session'),
actions: {
invalidateSession() {
this.get('session').invalidate();
}
}
});
13 changes: 13 additions & 0 deletions client/app/controllers/users/login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Ember from 'ember';

export default Ember.Controller.extend({
session: Ember.inject.service('session'),
actions : {
authenticate() {
let { username, password } = this.getProperties('username', 'password');
this.get('session').authenticate('authenticator:oauth2', username, password).catch((reason) => {
this.set('errorMessage', reason.error || reason);
});
}
}
});
7 changes: 6 additions & 1 deletion client/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ const Router = Ember.Router.extend({
});

Router.map(function() {
this.route('root', {path: '/'});
this.route('home');

this.route('users', function() {
this.route('login');
this.route('signup');
});
});

export default Router;
4 changes: 4 additions & 0 deletions client/app/routes/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Ember from 'ember';
import ApplicationRouteMixin from 'ember-simple-auth/mixins/application-route-mixin';

export default Ember.Route.extend(ApplicationRouteMixin);
8 changes: 8 additions & 0 deletions client/app/routes/home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Ember from 'ember';
import AuthenticatedRouteMixin from 'ember-simple-auth/mixins/authenticated-route-mixin';

export default Ember.Route.extend(AuthenticatedRouteMixin, {
model() {
return this.store.findAll('comment');
}
});
7 changes: 0 additions & 7 deletions client/app/routes/root.js

This file was deleted.

4 changes: 4 additions & 0 deletions client/app/routes/users/login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Ember from 'ember';
import UnauthenticatedRouteMixin from 'ember-simple-auth/mixins/unauthenticated-route-mixin';

export default Ember.Route.extend(UnauthenticatedRouteMixin);
5 changes: 5 additions & 0 deletions client/app/routes/users/signup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Ember from 'ember';
import UnauthenticatedRouteMixin from 'ember-simple-auth/mixins/unauthenticated-route-mixin';


export default Ember.Route.extend(UnauthenticatedRouteMixin);
14 changes: 13 additions & 1 deletion client/app/templates/application.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,20 @@
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Chitter Chatter</a>
{{#link-to 'application' class="navbar-brand"}}Chitter Chatter{{/link-to}}
</div>
<div id="navbar" class="navbar-collapse collapse">
{{#if session.isAuthenticated}}
<ul class="nav navbar-nav navbar-right">
<li><a {{action 'invalidateSession'}}>Logout</a></li>
</ul>
{{else}}
<ul class="nav navbar-nav navbar-right">
<li>{{#link-to 'users.login'}}Login{{/link-to}}</li>
<li>{{#link-to 'users.signup'}}Join{{/link-to}}</li>
</ul>
{{/if}}
</div><!--/.nav-collapse -->
{{!--
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{{outlet}}

{{#each model as |comment|}}
<p>
{{comment.text}}
Expand All @@ -10,3 +8,5 @@
</p>
{{/each}}
{{bs-input}}

{{outlet}}
10 changes: 10 additions & 0 deletions client/app/templates/users/login.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{{#bs-modal title="Login" body=false footer=false}}
{{#bs-modal-body}}
{{#bs-form action="authenticate" model=this}}
{{bs-form-element controlType="text" label="username" property="username"}}
{{bs-form-element action="authenticate" controlType="password" label="password" property="password"}}
{{/bs-form}}
{{/bs-modal-body}}
{{bs-modal-footer closeTitle="cancel" submitTitle="login"}}
{{/bs-modal}}
{{outlet}}
11 changes: 11 additions & 0 deletions client/app/templates/users/signup.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{#bs-modal title="Join us!" body=false footer=false}}
{{#bs-modal-body}}
{{#bs-form action="authenticate" model=this}}
{{bs-form-element controlType="text" label="username" property="username"}}
{{bs-form-element controlType="email" label="email" property="email"}}
{{bs-form-element action="authenticate" controlType="password" label="password" property="password"}}
{{/bs-form}}
{{/bs-modal-body}}
{{bs-modal-footer closeTitle="cancel" submitTitle="login"}}
{{/bs-modal}}
{{outlet}}
9 changes: 7 additions & 2 deletions client/config/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,22 @@ module.exports = function(environment) {
// e.g. 'with-controller': true
}
},

'ember-simple-auth': {
authenticationRoute: 'users.login',
routeAfterAuthentication: 'home',
routeIfAlreadyAuthenticated: 'home',
},
APP: {
// Here you can pass flags/options to your application instance
// when it is created
}
};


if (environment === 'development') {
// ENV.APP.LOG_RESOLVER = true;
// ENV.APP.LOG_ACTIVE_GENERATION = true;
// ENV.APP.LOG_TRANSITIONS = true;
ENV.APP.LOG_TRANSITIONS = true;
// ENV.APP.LOG_TRANSITIONS_INTERNAL = true;
// ENV.APP.LOG_VIEW_LOOKUPS = true;
}
Expand Down
2 changes: 2 additions & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@
"ember-data": "^2.4.0",
"ember-disable-proxy-controllers": "^1.0.1",
"ember-export-application-global": "^1.0.4",
"ember-i18n": "4.2.0",
"ember-load-initializers": "^0.5.0",
"ember-resolver": "^2.0.3",
"ember-simple-auth": "git+https://github.com/timini/ember-simple-auth.git",
"loader.js": "^4.0.0"
}
}
12 changes: 12 additions & 0 deletions client/tests/unit/controllers/application-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { moduleFor, test } from 'ember-qunit';

moduleFor('controller:application', 'Unit | Controller | application', {
// Specify the other units that are required for this test.
// needs: ['controller:foo']
});

// Replace this with your real tests.
test('it exists', function(assert) {
let controller = this.subject();
assert.ok(controller);
});
12 changes: 12 additions & 0 deletions client/tests/unit/controllers/root-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { moduleFor, test } from 'ember-qunit';

moduleFor('controller:root', 'Unit | Controller | root', {
// Specify the other units that are required for this test.
// needs: ['controller:foo']
});

// Replace this with your real tests.
test('it exists', function(assert) {
let controller = this.subject();
assert.ok(controller);
});
12 changes: 12 additions & 0 deletions client/tests/unit/controllers/users/login-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { moduleFor, test } from 'ember-qunit';

moduleFor('controller:users/login', 'Unit | Controller | users/login', {
// Specify the other units that are required for this test.
// needs: ['controller:foo']
});

// Replace this with your real tests.
test('it exists', function(assert) {
let controller = this.subject();
assert.ok(controller);
});
11 changes: 11 additions & 0 deletions client/tests/unit/routes/application-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { moduleFor, test } from 'ember-qunit';

moduleFor('route:application', 'Unit | Route | application', {
// Specify the other units that are required for this test.
// needs: ['controller:foo']
});

test('it exists', function(assert) {
let route = this.subject();
assert.ok(route);
});
11 changes: 11 additions & 0 deletions client/tests/unit/routes/users/login-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { moduleFor, test } from 'ember-qunit';

moduleFor('route:users/login', 'Unit | Route | users/login', {
// Specify the other units that are required for this test.
// needs: ['controller:foo']
});

test('it exists', function(assert) {
let route = this.subject();
assert.ok(route);
});
11 changes: 11 additions & 0 deletions client/tests/unit/routes/users/signup-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { moduleFor, test } from 'ember-qunit';

moduleFor('route:users/signup', 'Unit | Route | users/signup', {
// Specify the other units that are required for this test.
// needs: ['controller:foo']
});

test('it exists', function(assert) {
let route = this.subject();
assert.ok(route);
});
11 changes: 11 additions & 0 deletions client/tests/unit/routes/welcome-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { moduleFor, test } from 'ember-qunit';

moduleFor('route:welcome', 'Unit | Route | welcome', {
// Specify the other units that are required for this test.
// needs: ['controller:foo']
});

test('it exists', function(assert) {
let route = this.subject();
assert.ok(route);
});
Binary file modified server/api/db.sqlite3
Binary file not shown.
6 changes: 6 additions & 0 deletions server/api/main/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'oauth2_provider',

'users',
'chat',
Expand Down Expand Up @@ -141,3 +142,8 @@
),
'DEFAULT_METADATA_CLASS': 'rest_framework_json_api.metadata.JSONAPIMetadata',
}

OAUTH2_PROVIDER = {
# this is the list of available scopes
'SCOPES': {'read': 'Read scope', 'write': 'Write scope', 'groups': 'Access to your groups'}
}
3 changes: 2 additions & 1 deletion server/api/main/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^oauth/', include('oauth2_provider.urls', namespace='oauth2_provider')),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
url(r'^', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]
8 changes: 8 additions & 0 deletions server/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Django==1.9.4
django-braces==1.8.1
django-oauth-toolkit==0.10.0
djangorestframework==3.3.3
djangorestframework-jsonapi==2.0.0b2
inflection==0.3.1
oauthlib==1.0.3
six==1.10.0

0 comments on commit af71679

Please sign in to comment.