Meteor.js and the OpenId Connect user server

How to do authentication through a custom token server in Meteor.js?

Is there any package, such as accounts-google, for a user token server that handles authentication by simply accepting token endpoints, client ID, secrete, and scope as a configuration parameter.

+5
source share
1 answer

I do not know the general oauth package. But writing a package for your specific server is not so difficult, as there are several examples to watch.

As an example, you can use account-github, here is the code to connect to the client. Pay attention to the endpoint URL, client ID, scope, etc. This will handle the popup for you, but you probably want to include your own CSS:

var loginUrl = 'https://github.com/login/oauth/authorize' + '?client_id=' + config.clientId + '&scope=' + flatScope + '&redirect_uri=' + OAuth._redirectUri('github', config) + '&state=' + OAuth._stateParam(loginStyle, credentialToken); OAuth.launchLogin({ loginService: "github", loginStyle: loginStyle, loginUrl: loginUrl, credentialRequestCompleteCallback: credentialRequestCompleteCallback, credentialToken: credentialToken, popupOptions: {width: 900, height: 450} }); 

And here is a fragment from server , completing the process of obtaining an access token:

 var getAccessToken = function (query) { var config = ServiceConfiguration.configurations.findOne({service: 'github'}); if (!config) throw new ServiceConfiguration.ConfigError(); var response; try { response = HTTP.post( "https://github.com/login/oauth/access_token", { headers: { Accept: 'application/json', "User-Agent": userAgent }, params: { code: query.code, client_id: config.clientId, client_secret: OAuth.openSecret(config.secret), redirect_uri: OAuth._redirectUri('github', config), state: query.state } }); } catch (err) { throw _.extend(new Error("Failed to complete OAuth handshake with Github. " + err.message), {response: err.response}); } if (response.data.error) { // if the http response was a json object with an error attribute throw new Error("Failed to complete OAuth handshake with GitHub. " + response.data.error); } else { return response.data.access_token; } }; 

And using the token to get the user id:

 var getIdentity = function (accessToken) { try { return HTTP.get( "https://api.github.com/user", { headers: {"User-Agent": userAgent}, // http://developer.github.com/v3/#user-agent-required params: {access_token: accessToken} }).data; } catch (err) { throw _.extend(new Error("Failed to fetch identity from Github. " + err.message), {response: err.response}); } }; 

github and accounts-github packages should be very useful as links.

+5
source

Source: https://habr.com/ru/post/1214266/


All Articles