Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const OAuth2Strategy = require("passport-oauth2");
const passport = require("passport");
const config = require("./config");
const { decodeOpaqueId } = require("./lib/utils/decoding");
const logger = require("./lib/logger");
// This is needed to allow custom parameters (e.g. loginActions) to be included
// when requesting authorization. This is setup to allow only loginAction to pass through
OAuth2Strategy.prototype.authorizationParams = function (options = {}) {
return { loginAction: options.loginAction };
};
passport.use(
"oauth2",
new OAuth2Strategy(
{
authorizationURL: config.OAUTH2_AUTH_URL,
tokenURL: config.OAUTH2_TOKEN_URL,
clientID: config.OAUTH2_CLIENT_ID,
clientSecret: config.OAUTH2_CLIENT_SECRET,
callbackURL: config.OAUTH2_REDIRECT_URL,
state: true,
scope: ["offline"]
},
(accessToken, refreshToken, profile, cb) => {
// All resource server who would like to authorize from the oauth2orize server
// must use this strategy instead of OAuth2Strategy
// The Spec of OAuth2 defined 4 roles, which are user, resource server, client and authorization server.
// This file is part of **authorization server**
import OAuth2Strategy from "passport-oauth2";
import AccessTokenCollection from "../models/OAuth/AccessTokenCollection";
import AccessToken from "../models/OAuth/AccessToken";
import UserCollection from "../models/User/UserCollection";
import UserDocument from "../models/User/UserDocument";
import User from "../../client/core/src/models/User";
OAuth2Strategy.prototype.userProfile = (token: string, done: (err?: Error | null, profile?: User) => void) => {
AccessTokenCollection.findOne(
{token: token},
(error: Error, accessToken: AccessToken): void => {
if (error || !accessToken) {
done(error);
}
UserCollection.findById(accessToken.userId, (error: Error, user: UserDocument): void => {
if (error || !user) {
done(error);
}
done(undefined, user);
});
}
);
};
Strategy.prototype.authenticate = function(req, options) {
if (req.query && req.query.error_code && !req.query.error) {
return this.error(new lineAuthorizationError(req.query.error_message, parseInt(req.query.error_code, 10)));
}
OAuth2Strategy.prototype.authenticate.call(this, req, options);
};