From 01ebe85b958118f055c99153b128bb30f64160f2 Mon Sep 17 00:00:00 2001 From: Steve Anderson / Steve Ronuken Date: Wed, 16 Jun 2021 13:42:25 +0100 Subject: [PATCH] Create authentication.js added the authentication.js file --- .../eveonline/authentication.js | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 server/modules/authentication/eveonline/authentication.js diff --git a/server/modules/authentication/eveonline/authentication.js b/server/modules/authentication/eveonline/authentication.js new file mode 100644 index 00000000..e9c17a30 --- /dev/null +++ b/server/modules/authentication/eveonline/authentication.js @@ -0,0 +1,60 @@ +const _ = require('lodash') +/* global WIKI */ + +// ------------------------------------ +// EveOnline Account +// ------------------------------------ + +const OAuth2Strategy = require('passport-oauth2').Strategy +var jwt = require('jsonwebtoken'); + + +module.exports = { + init (passport, conf) { + OAuth2Strategy.prototype.userProfile = function (accessToken, cb) { + try { + const payload = jwt.decode(accessToken); + var sub=payload.sub; + var chardetail=sub.split(":"); + var charid=chardetail[2]; + cb(null, { + id: charid, + displayName: payload.name, + email: charid+'@auth.eveonline.com', + }) + } catch (err) { + WIKI.logger.warn('Eve Online - Failed to parse user profile.') + cb(err) + } + } + + passport.use('eveonline', + new OAuth2Strategy({ + authorizationURL: `https://login.eveonline.com/v2/oauth/authorize/`, + tokenURL: `https://login.eveonline.com/v2/oauth/token`, + clientID: conf.clientId, + clientSecret: conf.clientSecret, + callbackURL: conf.callbackURL, + passReqToCallback: true, + state: true + }, async (req, accessToken, refreshToken, profile, cb) => { + try { + const user = await WIKI.models.users.processProfile({ + providerKey: req.params.strategy, + profile + }) + cb(null, user) + } catch (err) { + cb(err, null) + } + }) + ) + }, + logout (conf) { + if (!conf.logoutURL) { + return '/' + } else { + return conf.logoutURL + } + } +}