-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
auth, list-research-context: use kp credentials from session #7
- Loading branch information
Showing
6 changed files
with
85 additions
and
44 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 |
---|---|---|
@@ -1,8 +1,9 @@ | ||
import type { GetSessionDTO } from "../../dto/auth-dto"; | ||
import type { ExtractKPCredentialsDTO, GetSessionDTO } from "../../dto/auth-dto"; | ||
|
||
/** | ||
* Represents the output port for the authentication gateway. | ||
*/ | ||
export default interface AuthGatewayOutputPort{ | ||
getSession(): Promise<GetSessionDTO>; | ||
extractKPCredentials(): Promise<ExtractKPCredentialsDTO>; | ||
} |
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
46 changes: 38 additions & 8 deletions
46
src/lib/infrastructure/server/gateway/next-auth-gateway.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 |
---|---|---|
@@ -1,33 +1,63 @@ | ||
import { inject, injectable } from "inversify"; | ||
import { getServerSession, type NextAuthOptions } from "next-auth"; | ||
import { TSession } from "~/lib/core/entity/auth/session"; | ||
import { SessionSchema, TSession } from "~/lib/core/entity/auth/session"; | ||
import AuthGatewayOutputPort from "~/lib/core/ports/secondary/auth-gateway-output-port"; | ||
import { CONSTANTS } from "../config/ioc/server-ioc-symbols"; | ||
import { GetSessionDTO } from "~/lib/core/dto/auth-dto"; | ||
import { ExtractKPCredentialsDTO, GetSessionDTO } from "~/lib/core/dto/auth-dto"; | ||
|
||
@injectable() | ||
export default class NextAuthGateway implements AuthGatewayOutputPort{ | ||
export default class NextAuthGateway implements AuthGatewayOutputPort { | ||
constructor( | ||
@inject(CONSTANTS.NEXT_AUTH_OPTIONS) private authOptions: NextAuthOptions, | ||
) {} | ||
) { } | ||
|
||
async getSession(): Promise<GetSessionDTO> { | ||
const session = await getServerSession(this.authOptions); | ||
if (!session) { | ||
return { | ||
success: false, | ||
data: { | ||
notFound: true, | ||
message: "Session not found", | ||
notFound: true, | ||
message: "Session not found", | ||
}, | ||
}; | ||
} | ||
return { | ||
success: true, | ||
data: session as TSession | ||
|
||
}; | ||
} | ||
|
||
|
||
async extractKPCredentials(): Promise<ExtractKPCredentialsDTO> { | ||
const sessionDTO = await this.getSession(); | ||
if (!sessionDTO.success) { | ||
return { | ||
success: false, | ||
data: { | ||
message: "User not authenticated", | ||
}, | ||
}; | ||
} | ||
const session = sessionDTO.data; | ||
const schemaValidationResult = SessionSchema.safeParse(session); | ||
if (!schemaValidationResult.success) { | ||
return { | ||
success: false, | ||
data: { | ||
message: "Session schema validation failed", | ||
}, | ||
}; | ||
} | ||
|
||
const user = sessionDTO.data.user; | ||
|
||
return { | ||
success: true, | ||
data: { | ||
clientID: user.kp.client_id, | ||
xAuthToken: user.kp.auth_token, | ||
}, | ||
}; | ||
} | ||
} |