-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d95880a
commit 46697a2
Showing
32 changed files
with
563 additions
and
46 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
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,2 @@ | ||
export { OauthSessionTokenProps, OauthSessionToken } from './oauth-session-token'; | ||
export { OauthSessionTokenFactory } from './oauth-session-token.factory'; |
71 changes: 71 additions & 0 deletions
71
apps/server/src/modules/oauth/domain/do/oauth-session-token.factory.spec.ts
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,71 @@ | ||
import { ObjectId } from '@mikro-orm/mongodb'; | ||
import { JwtTestFactory } from '@shared/testing'; | ||
import { TokenInvalidLoggableException } from '../../loggable'; | ||
import { OauthSessionToken } from './oauth-session-token'; | ||
import { OauthSessionTokenFactory } from './oauth-session-token.factory'; | ||
|
||
describe(OauthSessionTokenFactory.name, () => { | ||
describe('build', () => { | ||
describe('when the refresh token is valid', () => { | ||
const setup = () => { | ||
const userId = new ObjectId().toHexString(); | ||
const systemId = new ObjectId().toHexString(); | ||
const expiryDate = new Date(); | ||
const refreshToken = JwtTestFactory.createJwt({ exp: expiryDate.getTime() }); | ||
|
||
return { | ||
userId, | ||
systemId, | ||
expiryDate, | ||
refreshToken, | ||
}; | ||
}; | ||
|
||
it('should create the token object', () => { | ||
const { userId, systemId, expiryDate, refreshToken } = setup(); | ||
|
||
const result = OauthSessionTokenFactory.build({ | ||
userId, | ||
systemId, | ||
refreshToken, | ||
}); | ||
|
||
expect(result).toEqual( | ||
new OauthSessionToken({ | ||
id: expect.any(String), | ||
systemId, | ||
userId, | ||
refreshToken, | ||
expiresAt: expiryDate, | ||
}) | ||
); | ||
}); | ||
}); | ||
|
||
describe('when the refresh token is invalid', () => { | ||
const setup = () => { | ||
const userId = new ObjectId().toHexString(); | ||
const systemId = new ObjectId().toHexString(); | ||
const refreshToken = 'invalidOidcToken'; | ||
|
||
return { | ||
userId, | ||
systemId, | ||
refreshToken, | ||
}; | ||
}; | ||
|
||
it('should create the token object', () => { | ||
const { userId, systemId, refreshToken } = setup(); | ||
|
||
expect(() => | ||
OauthSessionTokenFactory.build({ | ||
userId, | ||
systemId, | ||
refreshToken, | ||
}) | ||
).toThrow(TokenInvalidLoggableException); | ||
}); | ||
}); | ||
}); | ||
}); |
23 changes: 23 additions & 0 deletions
23
apps/server/src/modules/oauth/domain/do/oauth-session-token.factory.ts
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,23 @@ | ||
import { ObjectId } from '@mikro-orm/mongodb'; | ||
import { EntityId } from '@shared/domain/types'; | ||
import jwt, { JwtPayload } from 'jsonwebtoken'; | ||
import { TokenInvalidLoggableException } from '../../loggable'; | ||
import { OauthSessionToken } from './oauth-session-token'; | ||
|
||
export class OauthSessionTokenFactory { | ||
public static build(params: { userId: EntityId; systemId: EntityId; refreshToken: string }): OauthSessionToken { | ||
const decodedRefreshToken: JwtPayload | null = jwt.decode(params.refreshToken, { json: true }); | ||
|
||
if (!decodedRefreshToken?.exp) { | ||
throw new TokenInvalidLoggableException(); | ||
} | ||
|
||
const oauthSessionToken = new OauthSessionToken({ | ||
...params, | ||
id: new ObjectId().toHexString(), | ||
expiresAt: new Date(decodedRefreshToken.exp), | ||
}); | ||
|
||
return oauthSessionToken; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
apps/server/src/modules/oauth/domain/do/oauth-session-token.ts
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,30 @@ | ||
import { AuthorizableObject, DomainObject } from '@shared/domain/domain-object'; | ||
import { EntityId } from '@shared/domain/types'; | ||
|
||
export interface OauthSessionTokenProps extends AuthorizableObject { | ||
userId: EntityId; | ||
|
||
systemId: EntityId; | ||
|
||
refreshToken: string; | ||
|
||
expiresAt: Date; | ||
} | ||
|
||
export class OauthSessionToken extends DomainObject<OauthSessionTokenProps> { | ||
get userId(): EntityId { | ||
return this.props.userId; | ||
} | ||
|
||
get systemId(): EntityId { | ||
return this.props.systemId; | ||
} | ||
|
||
get refreshToken(): string { | ||
return this.props.refreshToken; | ||
} | ||
|
||
get expiresAt(): Date { | ||
return this.props.expiresAt; | ||
} | ||
} |
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 @@ | ||
export { OauthSessionToken, OauthSessionTokenProps, OauthSessionTokenFactory } from './do'; |
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 @@ | ||
export { OauthSessionTokenEntityProps, OauthSessionTokenEntity } from './oauth-session-token.entity'; |
44 changes: 44 additions & 0 deletions
44
apps/server/src/modules/oauth/entity/oauth-session-token.entity.ts
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,44 @@ | ||
import { Entity, Index, ManyToOne, Property } from '@mikro-orm/core'; | ||
import { SystemEntity } from '@modules/system/entity'; | ||
import { BaseEntityWithTimestamps } from '@shared/domain/entity/base.entity'; | ||
import { User } from '@shared/domain/entity/user.entity'; | ||
import { EntityId } from '@shared/domain/types'; | ||
|
||
export interface OauthSessionTokenEntityProps { | ||
id?: EntityId; | ||
|
||
user: User; | ||
|
||
system: SystemEntity; | ||
|
||
refreshToken: string; | ||
|
||
expiresAt: Date; | ||
} | ||
|
||
@Entity({ tableName: 'oauth-session-token' }) | ||
export class OauthSessionTokenEntity extends BaseEntityWithTimestamps { | ||
@ManyToOne(() => User) | ||
user: User; | ||
|
||
@ManyToOne(() => SystemEntity) | ||
system: SystemEntity; | ||
|
||
@Property() | ||
refreshToken: string; | ||
|
||
@Index({ options: { expireAfterSeconds: 0 } }) | ||
@Property() | ||
expiresAt: Date; | ||
|
||
constructor(props: OauthSessionTokenEntityProps) { | ||
super(); | ||
if (props.id) { | ||
this.id = props.id; | ||
} | ||
this.user = props.user; | ||
this.system = props.system; | ||
this.refreshToken = props.refreshToken; | ||
this.expiresAt = props.expiresAt; | ||
} | ||
} |
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,3 +1,4 @@ | ||
export * from './interface'; | ||
export * from './oauth.module'; | ||
export * from './service'; | ||
export * from './domain'; |
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
Oops, something went wrong.