https://github.com/rocketchat/rocketchat-oauth2-server
OAuth 2 Server package
https://github.com/rocketchat/rocketchat-oauth2-server
Last synced: 3 months ago
JSON representation
OAuth 2 Server package
- Host: GitHub
- URL: https://github.com/rocketchat/rocketchat-oauth2-server
- Owner: RocketChat
- License: mit
- Created: 2015-12-31T18:43:28.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2022-11-08T07:20:40.000Z (about 3 years ago)
- Last Synced: 2024-05-01T11:29:38.226Z (over 1 year ago)
- Language: CoffeeScript
- Size: 23.4 KB
- Stars: 32
- Watchers: 15
- Forks: 27
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- Changelog: HISTORY.md
- License: LICENSE
Awesome Lists containing this project
README
# oauth2-server
This package is a implementation of the package [node-oauth2-server](https://github.com/thomseddon/node-oauth2-server) for Meteor.
It implements the `authorization_code` and works like the Facebook's OAuth popup.
## Install
```
meteor add rocketchat:oauth2-server
```
## Implementation
### Server implementation
* Initialize the lib
* Add routes to the default router
* Implement an authenticated route
`server/oauth2server.js`
```javascript
var oauth2server = new OAuth2Server({
// You can change the collection names, the values
// below are the default values.
accessTokensCollectionName: 'oauth_access_tokens',
refreshTokensCollectionName: 'oauth_refresh_tokens',
clientsCollectionName: 'oauth_clients',
authCodesCollectionName: 'oauth_auth_codes',
// You can pass the collection object too
// accessTokensCollection: new Meteor.Collection('custom_oauth_access_tokens'),
// refreshTokensCollection: new Meteor.Collection('custom_oauth_refresh_tokens'),
// clientsCollection: new Meteor.Collection('custom_oauth_clients'),
// authCodesCollection: new Meteor.Collection('custom_oauth_auth_codes'),
// You can enable some logs too
debug: true
});
// Add the express routes of OAuth before the Meteor routes
WebApp.rawConnectHandlers.use(oauth2server.app);
// Add a route to return account information
oauth2server.routes.get('/account', oauth2server.oauth.authorise(), function(req, res, next) {
var user = Meteor.users.findOne(req.user.id);
res.send({
id: user._id,
name: user.name
});
});
```
### Client/Pupup implementation
`client/authorize.js`
```javascript
// Define the route to render the popup view
FlowRouter.route('/oauth/authorize', {
action: function(params, queryParams) {
BlazeLayout.render('authorize', queryParams);
}
});
// Subscribe the list of already authorized clients
// to auto accept
Template.authorize.onCreated(function() {
this.subscribe('authorizedOAuth');
});
// Get the login token to pass to oauth
// This is the best way to identify the logged user
Template.authorize.helpers({
getToken: function() {
return localStorage.getItem('Meteor.loginToken');
}
});
// Auto click the submit/accept button if user already
// accepted this client
Template.authorize.onRendered(function() {
var data = this.data;
this.autorun(function(c) {
var user = Meteor.user();
if (user && user.oauth && user.oauth.authorizedClients && user.oauth.authorizedClients.indexOf(data.client_id()) > -1) {
c.stop();
$('button').click();
}
});
});
```
`client/authorize.html`
```html
{{#if currentUser}}
Authorise
Authorise
{{#unless Template.subscriptionsReady}}
loading...
{{/unless}}
{{else}}
{{> loginButtons}}
{{/if}}
```
`client/style.css`
```css
.hidden {
display: none;
}
```