Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
@universal-packages/authentication
Advanced tools
Dynamic authentication api base to bu plugged in with dynamic api modules to shape any custom authentication system. It comes with a default modules to handle a very simp,e email password authentication system, but it can be extended or replaced for a more sophisticated one.
npm install @universal-packages/authentication
The Authentication
class is a descendant of DynamicApi
class, it is the entry interface to load and perform all our authentication related dynamics.
import { Authentication } from '@universal-packages/authentication'
const authentication = new Authentication({ dynamicsLocation: './src', secret: 'my secret' })
await authentication.loadDynamics()
const result = await authentication.performDynamic('log-in', { email: 'david@universal-packages.com', password: '12345678' })
console.log(result)
// > { status: 'success', user: { id: 69, username: 'username', createdAt: [Date] } }
To override override parts of the default system just create non default dynamics in your dynamics location with the extension prefix auth-dynamic
, ex: LogIn.auth-dynamic.js
and export ad default dynamic class there. More about all the dynamics that can be override below.
Authentication take options similar to DynamicApi options:
debug
Boolean
If true the instance of this authentication dynamic api will keep track of what is being performed into a log.
dynamicsLocation
Required
String
Where to look up for dynamics to load withe to override default ones or new ones.
secret
Required
String
Secret used for cryptography to generate tokens and verifying them for this authentication instance in particular.
modules
Map
Authentication api modules to be enabled and configured
oneTimePassword
OneTimePasswordOptions
Options to configure how one time passwords are generated and validated. Check The options here
defaultModule
DynamicApiModule
enabled
Boolean
Whether the module is enabled or not.options
Object
emailValidation
Object
matcher
Regexp
Custom matcher to validate email.size
Object
min
Number
Minimum size of the email.max
Number
Maximum size of the email.passwordValidation
Object
size
Object
min
Number
Minimum size of the password.max
Number
Maximum size of the password.These dynamics are required to be override to have a fully functional authentication system.
Logic to create a new user with the given attributes.
PAYLOAD
Object
attributes
UserAttributes
RESULT
User
PAYLOAD
Object
user
User
attributes
UserAttributes
RESULT
void
PAYLOAD
Object
id
String | Number | BigInt
RESULT
User
These dynamics are not required to be override but can be to have a more sophisticated authentication system and they provide good default behavior.
It encrypts a password to be stored in the database.
PAYLOAD
Object
password
String
RESULT
String
Generates a secret for a concern. Hopeful to be used to generate tokens for a concern
PAYLOAD
Object
concern
String
identifier
String
RESULT
String
Generates a one time password for a concern and identifier. For example to be used to reset a password.
PAYLOAD
Object
concern
String
identifier
String
RESULT
String
Verifies a one time password for a concern and identifier. For example to be used to reset a password.
PAYLOAD
Object
concern
String
identifier
String
oneTimePassword
String
RESULT
Boolean
Async
Verifies email and password and if all configured behavior is positive it returns the user for which the credentials matched.
const result = authentication.perform('log-in', { email: 'david@universal-packages.com', password: 'password' })
PAYLOAD
Object
email
String
password
String
RESULT
AuthenticationResult
user
User
message?
invalid-credentials
failure
Async
Generates a password reset and performs the send-password-reset
dynamic passing the one time password.
const result = authentication.perform('request-password-reset', { email: 'david@u-p.com' })
PAYLOAD
Object
email
String
RESULT
AuthenticationResult
message?
nothing-to-do
`warning``Async
Validates sign up attributes and creates the new user.
const result = authentication.perform('sign-up', { email: 'some email', password: 'some password' })
PAYLOAD
Object
email
String
password
String
RESULT
AuthenticationResult
user
User
validation
ValidationResult
failure
valid
Boolean
errors
Object
<attribute>
String[]
Async
Validates and updates an user with new email and or password.
const result = authentication.perform('update-email-password', { email: 'new-email', user })
PAYLOAD
Object
user
User
email
String
password
String
RESULT
AuthenticationResult
user
User
validation
ValidationResult
failure
valid
Boolean
errors
Object
<attribute>
String[]
Async
Verifies a password reset and sets a new password
const result = authentication.perform('verify-password-reset', { email: 'email@email.com', oneTimePassword: '123456', password: 'new password' })
PAYLOAD
Object
email
String
oneTimePassword
String
password
String
RESULT
AuthenticationResult
validation
ValidationResult
failure
valid
Boolean
errors
Object
<attribute>
String[]
message?
invalid-one-time-password
failure
file file file file file file file file file
These dynamics are used to extend the default module dynamics, they are called on specific points while logging in and signing up.
When the user is not found while logging in. Write your custom dynamic to handle this case.
PAYLOAD
Object
email
String
RESULT
void
When the log in fails. Write your custom dynamic to handle this case.
PAYLOAD
Object
user
User
RESULT
void
When the log in is successful. Write your custom dynamic to handle this case.
PAYLOAD
Object
user
User
RESULT
void
When the sign up fails. Write your custom dynamic to handle this case.
PAYLOAD
Object
email
String
password
String
validation
ValidationResult
RESULT
void
When the sign up is successful. Write your custom dynamic to handle this case.
PAYLOAD
Object
user
User
RESULT
void
When the update is successful. Write your custom dynamic to handle this case.
PAYLOAD
Object
user
User
RESULT
void
When the user is found while logging in. Write your custom dynamic to handle this case and return true if you want to continue with the default behavior.
PAYLOAD
Object
user
User
RESULT
Boolean
Before logging in. Write your custom dynamic to handle this case and return true if you want to continue with the default behavior.
PAYLOAD
Object
email
String
password
String
RESULT
Boolean
Before signing up. Write your custom dynamic to handle this case and return true if you want to continue with the default behavior.
PAYLOAD
Object
email
String
password
String
Dynamics that you need to override to have a fully functional default module.
PAYLOAD
Object
email
String
RESULT
User
PAYLOAD
Object
email
String
RESULT
Boolean
PAYLOAD
Object
user
User
oneTimePassword
String
RESULT
void
PAYLOAD
Object
user
User
RESULT
void
PAYLOAD
Object
user
User
RESULT
void
PAYLOAD
Object
password
String
encryptedPassword
String
RESULT
Boolean
PAYLOAD
Object
user
User
RESULT
String
PAYLOAD
Object
user
User
RESULT
String
PAYLOAD
Object
password
String
RESULT
ValidationResult
valid
Boolean
errors
Object
<attribute>
String[]
PAYLOAD
Object
email
String
password
String
RESULT
ValidationResult
valid
Boolean
errors
Object
<attribute>
String[]
PAYLOAD
Object
email
String
password
String
RESULT
ValidationResult
valid
Boolean
errors
Object
<attribute>
String[]
Encrypt([propertyToEncrypt: string])
Use this decorator to automatically encrypt attributes in a class. For example for the password
attribute, when decorated, every time is set, the encryptedPassword
attribute is going to set with a hashed and salted string based on the password. It sets depending on the base attribute name encrypted<Attribute>
.
import { Encrypt } from '@universal-packages/authentication'
export default class User {
@Encrypt()
secret
encryptedSecret
}
const user = new User()
user.secret = 'my password'
console.log(user.secret, user.encryptedSecret)
// > undefined C49HSl4okw8yoCKfoNRnsqD4T0T6SJZkdpTgU1o478Mk4GT995KV5HUKzvsnN1fShOo9sdDQq3Rjiz+Brj9bCIeJfWrt7tMl936wWyBARkPCdDlj9OfLNNDnhGo7dkmbU8YBfpgcmoMUmCuIftupOik+Nk/Eu83J4epW5y2w0fM=
You can also specify the attribute name to store the hashed password.
import { Encrypt } from '@universal-packages/authentication'
export default class User {
@Encrypt('hashedSecret')
secret
hashedSecret
}
This library is developed in TypeScript and shipped fully typed.
The development of this library happens in the open on GitHub, and we are grateful to the community for contributing bugfixes and improvements. Read below to learn how you can take part in improving this library.
FAQs
Dynamic authentication api
We found that @universal-packages/authentication demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers 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’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.