Skip to content

SudoSOS Back-end API / internal/services / AuthenticationService

Class: AuthenticationService ​

The authentication service is responsible for verifying user authentications and handing out json web tokens.

Extends ​

Constructors ​

Constructor ​

ts
new AuthenticationService(manager?): AuthenticationService;

Parameters ​

ParameterType
manager?EntityManager

Returns ​

AuthenticationService

Inherited from ​

WithManager.constructor

Properties ​

PropertyModifierTypeInherited from
managerprotectedEntityManagerWithManager.manager

Methods ​

compareHash() ​

ts
compareHash(password, hash): Promise<boolean>;

Parameters ​

ParameterType
passwordstring
hashstring

Returns ​

Promise<boolean>


createResetToken() ​

ts
createResetToken(user): Promise<ResetTokenInfo>;

Creates a ResetToken for the given user.

Parameters ​

ParameterTypeDescription
userUser-

Returns ​

Promise<ResetTokenInfo>


createUserAndBind() ​

ts
createUserAndBind(ADUser): Promise<User>;

Creates a new User and binds it to the ObjectGUID of the provided LDAPUser. Function is ran in a single DB transaction in the context of an EntityManager

Parameters ​

ParameterTypeDescription
ADUserLDAPUserThe user for which to create a new account.

Returns ​

Promise<User>


getMemberAuthenticators() ​

ts
getMemberAuthenticators(user): Promise<User[]>;

Get a list of all organs this user is a member of.

Parameters ​

ParameterTypeDescription
userUser-

Returns ​

Promise<User[]>


getSaltedToken() ​

ts
getSaltedToken(params): Promise<AuthenticationResult>;

Created a salted JWT token for the given userId.

Parameters ​

ParameterTypeDescription
params{ context: AuthenticationContext; expiry?: number; posId?: number; salt?: string; user: User; }-
params.contextAuthenticationContextAuthentication context with role manager and token handler
params.expiry?numberCustom expiry time (in seconds). If not set, the default tokenHandler expiry will be used
params.posId?numberOptional POS identifier. If provided, creates a lesser token (restricted access).
params.salt?stringOptional salt for token generation. If not provided, a new salt will be generated.
params.userUserThe user for which to generate the token

Returns ​

Promise<AuthenticationResult>


HashAuthentication() ​

ts
HashAuthentication<T>(
   pass, 
   authenticator, 
   context, 
posId?): Promise<AuthenticationResult>;

Authenticates the account against a local password

Type Parameters ​

Type Parameter
T extends HashBasedAuthenticationMethod

Parameters ​

ParameterTypeDescription
passstringThe provided password
authenticatorTThe stored authentication
contextAuthenticationContextAuthenticationContext to use
posId?numberOptional POS identifier. If provided, creates a lesser token (restricted access).

Returns ​

Promise<AuthenticationResult>


hashPassword() ​

ts
hashPassword(password): Promise<string>;

Helper function hashes the given string with salt.

Parameters ​

ParameterTypeDescription
passwordstringpassword to hash

Returns ​

Promise<string>


hashPinPassword() ​

ts
hashPinPassword(pin): Promise<string>;

Helper function hashes a PIN with fewer rounds for better performance.

Parameters ​

ParameterTypeDescription
pinstringPIN code to hash

Returns ​

Promise<string>


isResetTokenRequestValid() ​

ts
isResetTokenRequestValid(request): Promise<ResetToken>;

Function that checks if the provided request corresponds to a valid reset token in the DB.

Parameters ​

ParameterTypeDescription
requestAuthenticationResetTokenRequest-

Returns ​

Promise<ResetToken>


LDAPAuthentication() ​

ts
LDAPAuthentication(
   uid, 
   password, 
onNewUser): Promise<User>;

Authenticates the account against the AD

Parameters ​

ParameterTypeDescription
uidstringThe AD account name.
passwordstringThe password user for authentication.
onNewUser(ADUser) => Promise<User>Callback function when user does not exist in local system.

Returns ​

Promise<User>


makeJsonWebToken() ​

ts
makeJsonWebToken(
   user, 
   roles, 
   organs, 
   overrideMaintenance, 
posId?): Promise<JsonWebToken>;

Creates the corresponding token-content of the given user in the given context.

Parameters ​

ParameterTypeDescription
userUserThe user for which to generate the token-content
rolesRole[]The roles this user has
organsUser[]The organs this user belongs to
overrideMaintenancebooleanIf the token should be able to access all endpoints in maintenance mode
posId?numberOptional POS identifier. If provided, creates a lesser token (restricted access).

Returns ​

Promise<JsonWebToken>


resetLocalUsingToken() ​

ts
resetLocalUsingToken(
   resetToken, 
   token, 
newPassword): Promise<LocalAuthenticator>;

Resets the user local authenticator if token matches stored hash

Parameters ​

ParameterTypeDescription
resetTokenResetTokenThe stored reset token
tokenstringPasscode of the reset token
newPasswordstringNew password to set for the authentication

Returns ​

Promise<LocalAuthenticator>


setMemberAuthenticator() ​

ts
setMemberAuthenticator(users, organ): Promise<void>;

Gives the array of users membership to the organ. Used for shared accounts. Note that this replaces the existing memberships for this organ.

Parameters ​

ParameterTypeDescription
usersUser[]The users that gain membership.
organUserThe organ account that the users become members of.

Returns ​

Promise<void>


setUserAuthenticationHash() ​

ts
setUserAuthenticationHash<T>(
   user, 
   pass, 
Type): Promise<T>;

Generic function that sets a hash authentication of a user. If the user has no Authentication set it will create the authentication.

Type Parameters ​

Type Parameter
T extends HashBasedAuthenticationMethod

Parameters ​

ParameterTypeDescription
userUserThe user for which to set the authentication
passstringCode to set
Type() => T-

Returns ​

Promise<T>


setUserAuthenticationNfc() ​

ts
setUserAuthenticationNfc<T>(
   user, 
   nfcCode, 
Type): Promise<T>;

Type Parameters ​

Type Parameter
T extends NfcAuthenticator

Parameters ​

ParameterType
userUser
nfcCodestring
Type() => T

Returns ​

Promise<T>


asAuthenticationResponse() ​

ts
static asAuthenticationResponse(
   user, 
   roles, 
   organs, 
   token): AuthenticationResponse;

Converts the internal object representation to an authentication response, which can be returned in the API response.

Parameters ​

ParameterTypeDescription
userUserThe user that authenticated.
rolesRole[]The roles that the authenticated user has.
organsUser[]The organs that the user is part of.
tokenstringThe JWT token that can be used to authenticate.

Returns ​

AuthenticationResponse

The authentication response.


isTokenExpired() ​

ts
static isTokenExpired(resetToken): boolean;

Parameters ​

ParameterType
resetTokenResetToken

Returns ​

boolean