Giter Site home page Giter Site logo

rocketchat-oauth2-server's Introduction

oauth2-server

This package is a implementation of the package 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

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

// 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

<template name="authorize">
  {{#if currentUser}}
    <form method="post" action="" role="form" class="{{#unless Template.subscriptionsReady}}hidden{{/unless}}">
      <h2>Authorise</h2>
      <input type="hidden" name="allow" value="yes">
      <input type="hidden" name="token" value="{{getToken}}">
      <input type="hidden" name="client_id" value="{{client_id}}">
      <input type="hidden" name="redirect_uri" value="{{redirect_uri}}">
      <input type="hidden" name="response_type" value="code">
      <button type="submit">Authorise</button>
    </form>
    {{#unless Template.subscriptionsReady}}
      loading...
    {{/unless}}
  {{else}}
    {{> loginButtons}}
  {{/if}}
</template>

client/style.css

.hidden {
  display: none;
}

rocketchat-oauth2-server's People

Contributors

engelgabriel avatar graywolf336 avatar pagebakers avatar pierre-lehnen-rc avatar rodrigok avatar sampaiodiego avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rocketchat-oauth2-server's Issues

Cannot set headers after they are sent to the client

Exception in callback of async function: Error [ERR_HTTP_HEADERS_SENT] [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

I get this error when trying to access [GET] /oauth/authorize.
What happening ?

Oauth error access url oauth/authorize : get Redirect URL does not match

Description:

When I access url /oauth/authorize always redirect to /oauth/error/invalid_redirect_uri and get text message Redirect URL does not match.
What is happening ?

Steps to reproduce:

  1. Go to setting
  2. Click on Oauth Apps
  3. Iam create oauth app for get client id and key
  4. Use that for login to my app with rocket chat.
  5. And when click button login with rocket chat . BOOM get error like description above

Expected behavior:

I hope it works

Actual behavior:

error is:
"error": "Redirect URL does not match",
"error_description": "The redirect URI provided is missing or does not match",
"error_uri": "MY-DOMAIN.COM/oauth/error/invalid_redirect_uri"

Server Setup Information:

  • Version of Rocket.Chat Server: 3.4.1
  • Operating System: Ubuntu Server 18.04
  • Deployment Method: direct install
  • Number of Running Instances: 1
  • DB Replicaset Oplog: n/a
  • NodeJS Version: 12.14.0
  • MongoDB Version: 3.6

Client Setup Information

  • Desktop App or Browser Version: chrome / firefox
  • Operating System: Win, Mac and Linux

Additional context

N/A

Relevant logs:

Nothing log

Missing bodyParsers?

This code is notable missing parsers for the request body. As per the documentation, this is required in order to extract authorization parameters. Results from my local testing show this to be an issue as well.

userinfo endpoint?

Trying to authenticate another app using rocketchat and get the user's email. What is the openid userinfo endpoint in rocketchat?

Error: has no method 'authorize'

Hi, I got this error while I run the example showed on README.

TypeError: Object [object Object] has no method 'authorize'
W20160304-16:16:35.383(8)? (STDERR)     at server/oauth2server.js:21:56
W20160304-16:16:35.383(8)? (STDERR)     at /home/ljm/simple-todos/.meteor/local/build/programs/server/app/server/oauth2server.js:39:4
W20160304-16:16:35.383(8)? (STDERR)     at /home/ljm/simple-todos/.meteor/local/build/programs/server/boot.js:242:10
W20160304-16:16:35.383(8)? (STDERR)     at Array.forEach (native)
W20160304-16:16:35.383(8)? (STDERR)     at Function._.each._.forEach (/root/.meteor/packages/meteor-tool/.1.1.10.ki0ccv++os.linux.x86_64+web.browser+web.cordova/mt-os.linux.x86_64/dev_bundle/server-lib/node_modules/underscore/underscore.js:79:11)
W20160304-16:16:35.384(8)? (STDERR)     at /home/ljm/simple-todos/.meteor/local/build/programs/server/boot.js:137:5

It seems this error is caused by this function:

oauth2server.routes.get('/account', oauth2server.oauth.authorize(), function(req, res, next) {
  var user = Meteor.users.findOne(req.user.id);

  res.send({
    id: user._id,
    name: user.name
  });
});

I am so confused that authorize has been implemented by node-oauth2-server , why the error occurred ?

๐Ÿ˜† So so appreciate for any idea !

Install from `package.js` get old version

Hi, we get the version 1.0.1 (which does not work when passing a custom collection for oauth clients) when installing the package by adding it to our package.js instead of 2.0.0 (which works perfectly).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.