Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resets has_pending_invitation flag #139

Merged
merged 2 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions __test__/auth/CompositeLogInHandler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { CompositeLogInHandler } from "../../src/features/auth/domain"

test("It invokes all log in handlers for user", async () => {
let userId1: string | undefined
let userId2: string | undefined
let userId3: string | undefined
const sut = new CompositeLogInHandler([{
async handleLogIn(userId) {
userId1 = userId
}
}, {
async handleLogIn(userId) {
userId2 = userId
}
}, {
async handleLogIn(userId) {
userId3 = userId
}
}])
await sut.handleLogIn("1234")
expect(userId1).toEqual("1234")
expect(userId2).toEqual("1234")
expect(userId3).toEqual("1234")
})
18 changes: 18 additions & 0 deletions __test__/auth/RemoveInvitedFlagLogInHandler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { RemoveInvitedFlagLogInHandler } from "../../src/features/auth/domain"

test("It removes invited flag from specified user", async () => {
let updatedUserId: string | undefined
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
let updatedMetadata: {[key: string]: any} | undefined
const sut = new RemoveInvitedFlagLogInHandler({
async updateMetadata(userId, metadata) {
updatedUserId = userId
updatedMetadata = metadata
}
})
await sut.handleLogIn("1234")
expect(updatedUserId).toEqual("1234")
expect(updatedMetadata).toEqual({
has_pending_invitation: false
})
})
36 changes: 22 additions & 14 deletions src/composition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
import {
GitHubOAuthTokenRefresher,
GitHubInstallationAccessTokenDataSource,
Auth0MetadataUpdater,
Auth0RefreshTokenReader,
Auth0RepositoryAccessReader,
Auth0UserIdentityProviderReader
Expand All @@ -30,6 +31,7 @@ import {
AccessTokenService,
CachingRepositoryAccessReaderConfig,
CachingUserIdentityProviderReader,
CompositeLogInHandler,
CompositeLogOutHandler,
CredentialsTransferringLogInHandler,
ErrorIgnoringLogOutHandler,
Expand All @@ -41,6 +43,7 @@ import {
LockingAccessTokenService,
OAuthTokenRepository,
OnlyStaleRefreshingAccessTokenService,
RemoveInvitedFlagLogInHandler,
RepositoryRestrictingAccessTokenDataSource,
UserDataCleanUpLogOutHandler
} from "@/features/auth/domain"
Expand Down Expand Up @@ -187,20 +190,25 @@ export const projectDataSource = new CachingProjectDataSource(
projectRepository
)

export const logInHandler = new CredentialsTransferringLogInHandler({
isUserGuestReader: new IsUserGuestReader(
userIdentityProviderReader
),
guestCredentialsTransferrer: new NullObjectCredentialsTransferrer(),
hostCredentialsTransferrer: new HostCredentialsTransferrer({
refreshTokenReader: new Auth0RefreshTokenReader({
...auth0ManagementCredentials,
connection: "github"
}),
oAuthTokenRefresher: gitHubOAuthTokenRefresher,
oAuthTokenRepository: oAuthTokenRepository
})
})
export const logInHandler = new CompositeLogInHandler([
new CredentialsTransferringLogInHandler({
isUserGuestReader: new IsUserGuestReader(
userIdentityProviderReader
),
guestCredentialsTransferrer: new NullObjectCredentialsTransferrer(),
hostCredentialsTransferrer: new HostCredentialsTransferrer({
refreshTokenReader: new Auth0RefreshTokenReader({
...auth0ManagementCredentials,
connection: "github"
}),
oAuthTokenRefresher: gitHubOAuthTokenRefresher,
oAuthTokenRepository: oAuthTokenRepository
})
}),
new RemoveInvitedFlagLogInHandler(
new Auth0MetadataUpdater({ ...auth0ManagementCredentials })
)
])

export const logOutHandler = new ErrorIgnoringLogOutHandler(
new CompositeLogOutHandler([
Expand Down
28 changes: 28 additions & 0 deletions src/features/auth/data/Auth0MetadataUpdater.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ManagementClient } from "auth0"

type Auth0MetadataUpdaterConfig = {
readonly domain: string
readonly clientId: string
readonly clientSecret: string
}

export default class Auth0MetadataUpdater {
private readonly managementClient: ManagementClient

constructor(config: Auth0MetadataUpdaterConfig) {
this.managementClient = new ManagementClient({
domain: config.domain,
clientId: config.clientId,
clientSecret: config.clientSecret
})
}

/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
async updateMetadata(userId: string, metadata: {[key: string]: any}): Promise<void> {
await this.managementClient.users.update({
id: userId
}, {
app_metadata: metadata
})
}
}
1 change: 1 addition & 0 deletions src/features/auth/data/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { default as Auth0MetadataUpdater } from "./Auth0MetadataUpdater"
export { default as Auth0RefreshTokenReader } from "./Auth0RefreshTokenReader"
export { default as Auth0RepositoryAccessReader } from "./Auth0RepositoryAccessReader"
export { default as Auth0UserIdentityProviderReader } from "./Auth0UserIdentityProviderReader"
Expand Down
14 changes: 14 additions & 0 deletions src/features/auth/domain/logIn/CompositeLogInHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import ILogInHandler from "./ILogInHandler"

export default class CompositeLogInHandler implements ILogInHandler {
private readonly handlers: ILogInHandler[]

constructor(handlers: ILogInHandler[]) {
this.handlers = handlers
}

async handleLogIn(userId: string): Promise<void> {
const promises = this.handlers.map(e => e.handleLogIn(userId))
await Promise.all(promises)
}
}
20 changes: 20 additions & 0 deletions src/features/auth/domain/logIn/RemoveInvitedFlagLogInHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import ILogInHandler from "./ILogInHandler"

export interface IMetadataUpdater {
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
updateMetadata(userId: string, metadata: {[key: string]: any}): Promise<void>
}

export default class RemoveInvitedFlagLogInHandler implements ILogInHandler {
private readonly metadataUpdater: IMetadataUpdater

constructor(metadataUpdater: IMetadataUpdater) {
this.metadataUpdater = metadataUpdater
}

async handleLogIn(userId: string): Promise<void> {
await this.metadataUpdater.updateMetadata(userId, {
has_pending_invitation: false
})
}
}
2 changes: 2 additions & 0 deletions src/features/auth/domain/logIn/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export { default as CompositeLogInHandler } from "./CompositeLogInHandler"
export { default as CredentialsTransferringLogInHandler } from "./CredentialsTransferringLogInHandler"
export type { default as ILogInHandler } from "./ILogInHandler"
export { default as RemoveInvitedFlagLogInHandler } from "./RemoveInvitedFlagLogInHandler"