Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
apollo-passport-local-strategy
Advanced tools
Local strategy using email address and hashed, bcrypted password
Forked from apollo-passport/local Local strategy using email address and hashed, bcrypted password.
Copyright (c) 2017 by Gilad Shoham, released under the MIT license.
npm install apollo-passport-mongodb-driver
First, make sure to install my version of local strategy:
npm i --save apollo-passport-local-strategy
In apollo-passport docs, you will see this line:
import { Strategy as LocalStrategy } from 'passport-local';
It should be replaced by:
import { Strategy as LocalStrategy } from 'apollo-passport-local-strategy/lib/index';
(If you don't do this, the options like hooks will not work)
const typeDefinitions = `
type RootMutation {
apCreateUserEmailPassword (input: apUserInput!): PassportResult,
apVerifyAccount (userId: String, verificationToken: String!): SimpleError,
apRecoverPasswordRequest (email: String): String,
apRecoverPassword (userId: String!, token: String!, newPassword: String!): String,
apUpdateUserPassword (userId: String!, oldPassword: String!, newPassword: String!): String,
apLoginEmailPassword (email: String!, password: String!): PassportResult
}
type SimpleError {
errCode: String,
errMessage: String
}
`;
You should define your own userInput type (named apUserInput). This way you can define what ever fields you want to be part of the registration process. You have to make sure that you have email and password fields, because the library used them internally.
Example:
`input apUserInput {
# User email
email: String!
# User password
password: String!
# User first name
firstName: String!
# User last name
lastName: String!
# office phone number
phone: String
# Personal mobile phone number
mobilePhone: String
}
`;
During create user the library will add these fields to the new user:
crypto.randomBytes(20, (err, buf) => {
var token = buf.toString('hex');
});
verificationTokenExpiration - An expiration to account verification token (Will be used during verify account), defalut to be 1 month. You can change it via configuration.
verified - Will be set to false during creation, and will be change to true on verify account.
A new mutation to generate tokens for reset password. The tokens will be generated the same way as the account verification token. The name of the token fields will be:
This new error currently used only on apVerifyAccount mutation. This will give you better way to handle those errors in the client side. List of the possible errors:
{
errCode: 'USER_NOT_EXIST',
errMessage: 'No such user id',
}
{
errCode: 'RESET_PASS_IN_PROGRESS',
errMessage: 'Reset password is in progress',
}
{
errCode: 'TOKEN_NOT_VALID',
errMessage: 'Verification token not valid',
}
{
errCode: 'TOKEN_EXPIRED',
errMessage: 'Verification token expired',
}
During the create user there is a check if the user exist. If the user exist but without any service the new user will be merged with the existing one. The reason beyond this, is if you collect some user details from other users or from external source, maybe you want someone to invite other user, you want to create this user, but still let him register and define his passowrd.
The hooks will be called with the user as argument. Default for tokensExpirationLength is 1 week for both (verification and reset pass). The length is at SECONDS and not milliseconds.
Here is an example for one hook and changing the tokens expiration length:
import { Strategy as LocalStrategy } from 'apollo-passport-local-strategy/lib/index';
const onRegisterUserHook = function(user){
logService.log('user registered');
mailService.sendVerificationMail(user);
}
const onLoginEndHook = function(user){
logService.log('user logged in');
UserService.updateLastLogin(user._id);
}
const MONTH = 60 * 60 * 24 * 7 * 4;
const apolloPassportLocalOptions = {
usernameField: 'email',
passwordField: 'password',
hookMethods: {
onCreateUserEnd: onRegisterUserHook,
onBeforeStoreRegisteredUser: onBeforeStoreRegisteredUserHook,
onRecoverPasswordRequestEnd: onRecoverPasswordRequestEndHook,
onRecoverPasswordEnd: onRecoverPasswordEndHook,
onUpdatePasswordEnd: onUpdatePasswordEndHook,
onVerifyAccountEnd: onVerifyAccountEndHook,
onLoginEnd: onLoginEndHook,
},
// Set the expiration to be 4 weeks
tokensExpirationLength: {
verification: MONTH,
resetPass: MONTH,
},
};
const apolloPassport = new ApolloPassport({
db: MongoDBDriver,
jwtSecret: 'my special secret',
authPath: '/ap-auth',
});
apolloPassport.use('local', LocalStrategy, apolloPassportLocalOptions);
See https://github.com/gadicc/apollo-passport.
Note: you don't usually need a special apollo-passport-xxx
package for every passport strategy. apollo-passport-local
is a special case because of it's dependencies, e.g. bcrypt
and some client-side hashing.
$ npm i --save passport-local apollo-passport-local-strategy
Server
import { Strategy as LocalStrategy } from 'apollo-passport-local-strategy/lib/index';
// Your previously created ApolloPassport instance...
apolloPassport.use('local', LocalStrategy /*, options */);
Client
import ApolloPassportLocal from 'apollo-passport-local-strategy/lib/client';
// Your previously created ApolloPassport instance...
apolloPassport.use('local', ApolloPassportLocal);
FAQs
Local strategy using email address and hashed, bcrypted password
We found that apollo-passport-local-strategy demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.