-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from victor-0x29a/patch/auth-service-for-abstr…
…action Patch: auth service
- Loading branch information
Showing
8 changed files
with
136 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { User } from "../models"; | ||
import { Auth } from "./auth.service"; | ||
|
||
const userData = { | ||
id: 1, | ||
name: 'foo', | ||
username: 'bar', | ||
password: '$----secret----$' | ||
} as unknown as User | ||
|
||
describe('should success in all flow methods', () => { | ||
const appSecret = '--key--' | ||
|
||
const service = new Auth(userData, appSecret) | ||
|
||
test('should get the sign payload', () => { | ||
const payloadExpected = { | ||
id: userData.id, | ||
username: userData.username | ||
} | ||
|
||
expect(payloadExpected).toEqual(service.tokenPayload) | ||
}) | ||
|
||
test('should get the token options', () => { | ||
const optionsExpected = { | ||
expiresIn: '20min' | ||
} | ||
|
||
expect(optionsExpected).toEqual(service.tokenOptions) | ||
}) | ||
|
||
test('should sign a token', () => { | ||
const tokenSigned = service.signToken() | ||
|
||
const tokenParts = tokenSigned.split('.').length | ||
|
||
expect(typeof tokenSigned).toEqual('string') | ||
expect(tokenParts).toBe(3) | ||
}) | ||
}) | ||
|
||
describe('should test all validation methods', () => { | ||
const generateService = (appSecret: undefined | number | object | null | string) => new Auth(userData, appSecret as unknown as string) | ||
|
||
const validationErrorMessage = 'The application secret must be provided.' | ||
|
||
test('should not sign a token when havent app secret', () => { | ||
const service = generateService(undefined) | ||
|
||
expect(() => service.signToken()).toThrow(validationErrorMessage) | ||
|
||
const serviceWithNullSecret = generateService(null) | ||
|
||
expect(() => serviceWithNullSecret.signToken()).toThrow(validationErrorMessage) | ||
|
||
const serviceWithEmptyString = generateService('') | ||
|
||
expect(() => serviceWithEmptyString.signToken()).toThrow(validationErrorMessage) | ||
}) | ||
|
||
test('shoult not sign a token when the app secret is a number', () => { | ||
const service = generateService(9) | ||
|
||
expect(() => service.signToken()).toThrow(validationErrorMessage) | ||
}) | ||
|
||
test('should not sign a token when the app secret is an object', () => { | ||
const service = generateService({}) | ||
|
||
expect(() => service.signToken()).toThrow(validationErrorMessage) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import jwt from 'jsonwebtoken' | ||
import type { User } from '../models' | ||
import { InternalValidation } from '../web/errors' | ||
|
||
class Auth { | ||
constructor (private readonly userData: User, private readonly applicationSecret: string) {} | ||
|
||
get tokenPayload () { | ||
return { | ||
id: this.userData.id, | ||
username: this.userData.username | ||
} | ||
} | ||
|
||
get tokenOptions () { | ||
return { | ||
expiresIn: '20min' | ||
} | ||
} | ||
|
||
private validate(): void { | ||
const haventApplicationSecret = !this.applicationSecret | ||
const applicationSecretIsString = typeof this.applicationSecret === 'string' | ||
|
||
if (haventApplicationSecret || !applicationSecretIsString) { | ||
throw new InternalValidation('The application secret must be provided.') | ||
} | ||
} | ||
|
||
public signToken () { | ||
this.validate() | ||
|
||
return jwt.sign(this.tokenPayload, this.applicationSecret, this.tokenOptions) | ||
} | ||
} | ||
|
||
export { Auth } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export { Environment as EnvironmentService } from "./evironment.service"; | ||
export { Environment as EnvironmentService } from "./environment.service"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters