[codex] add session context to credential errors (#3349)
This commit is contained in:
1 parent
61f8d46fa2
commit
82a9bcc7f4
2 files changed
+308
-153
No files matched your search
@@ -44,8 +44,8 @@ const failingSessionLookupRepositoryLayer = Layer.succeed(AuthSessions.AuthSessi
|
|||||||
create: () => Effect.void,
|
create: () => Effect.void,
|
||||||
getById: () => Effect.fail(repositoryFailure),
|
getById: () => Effect.fail(repositoryFailure),
|
||||||
listActive: () => Effect.succeed([]),
|
listActive: () => Effect.succeed([]),
|
||||||
revoke: () => Effect.succeed(false),
|
revoke: () => Effect.fail(repositoryFailure),
|
||||||
revokeAllExcept: () => Effect.succeed([]),
|
revokeAllExcept: () => Effect.fail(repositoryFailure),
|
||||||
setLastConnectedAt: () => Effect.void,
|
setLastConnectedAt: () => Effect.void,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -104,11 +104,29 @@ it.layer(NodeServices.layer)("SessionStore.layer", (it) => {
|
|||||||
|
|
||||||
const sessionError = yield* Effect.flip(sessions.verify(issued.token));
|
const sessionError = yield* Effect.flip(sessions.verify(issued.token));
|
||||||
const websocketError = yield* Effect.flip(sessions.verifyWebSocketToken(websocket.token));
|
const websocketError = yield* Effect.flip(sessions.verifyWebSocketToken(websocket.token));
|
||||||
|
const revokeError = yield* Effect.flip(sessions.revoke(issued.sessionId));
|
||||||
|
const revokeOthersError = yield* Effect.flip(sessions.revokeAllExcept(issued.sessionId));
|
||||||
|
|
||||||
expect(sessionError._tag).toBe("SessionCredentialVerificationError");
|
expect(sessionError._tag).toBe("SessionCredentialVerificationError");
|
||||||
expect(websocketError._tag).toBe("WebSocketTokenVerificationError");
|
expect(websocketError._tag).toBe("WebSocketTokenVerificationError");
|
||||||
expect(sessionError.cause).toBe(repositoryFailure);
|
expect(sessionError.cause).toBe(repositoryFailure);
|
||||||
expect(websocketError.cause).toBe(repositoryFailure);
|
expect(websocketError.cause).toBe(repositoryFailure);
|
||||||
|
if (sessionError._tag === "SessionCredentialVerificationError") {
|
||||||
|
expect(sessionError.sessionId).toBe(issued.sessionId);
|
||||||
|
}
|
||||||
|
if (websocketError._tag === "WebSocketTokenVerificationError") {
|
||||||
|
expect(websocketError.sessionId).toBe(issued.sessionId);
|
||||||
|
}
|
||||||
|
expect(revokeError).toMatchObject({
|
||||||
|
_tag: "SessionRevocationError",
|
||||||
|
sessionId: issued.sessionId,
|
||||||
|
cause: repositoryFailure,
|
||||||
|
});
|
||||||
|
expect(revokeOthersError).toMatchObject({
|
||||||
|
_tag: "OtherSessionsRevocationError",
|
||||||
|
currentSessionId: issued.sessionId,
|
||||||
|
cause: repositoryFailure,
|
||||||
|
});
|
||||||
}).pipe(Effect.provide(failingSessionLookupCredentialLayer)),
|
}).pipe(Effect.provide(failingSessionLookupCredentialLayer)),
|
||||||
);
|
);
|
||||||
it.effect("verifies session tokens against the Effect clock", () =>
|
it.effect("verifies session tokens against the Effect clock", () =>
|
||||||
@@ -145,7 +163,52 @@ it.layer(NodeServices.layer)("SessionStore.layer", (it) => {
|
|||||||
yield* TestClock.adjust(Duration.seconds(2));
|
yield* TestClock.adjust(Duration.seconds(2));
|
||||||
|
|
||||||
const error = yield* Effect.flip(sessions.verifyWebSocketToken(websocket.token));
|
const error = yield* Effect.flip(sessions.verifyWebSocketToken(websocket.token));
|
||||||
expect(error.message).toContain("expired");
|
expect(error._tag).toBe("WebSocketSessionExpiredError");
|
||||||
|
if (error._tag === "WebSocketSessionExpiredError") {
|
||||||
|
expect(error.sessionId).toBe(issued.sessionId);
|
||||||
|
expect(error.expiresAt.epochMilliseconds).toBe(issued.expiresAt.epochMilliseconds);
|
||||||
|
expect(error.observedAt.epochMilliseconds).toBeGreaterThan(
|
||||||
|
error.expiresAt.epochMilliseconds,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}).pipe(Effect.provide(Layer.merge(makeSessionStoreLayer(), TestClock.layer()))),
|
||||||
|
);
|
||||||
|
|
||||||
|
it.effect("includes expiry context when session and websocket tokens expire", () =>
|
||||||
|
Effect.gen(function* () {
|
||||||
|
const sessions = yield* SessionStore.SessionStore;
|
||||||
|
const issued = yield* sessions.issue({
|
||||||
|
method: "bearer-access-token",
|
||||||
|
subject: "short-lived-token",
|
||||||
|
ttl: Duration.seconds(1),
|
||||||
|
});
|
||||||
|
const websocket = yield* sessions.issueWebSocketToken(issued.sessionId, {
|
||||||
|
ttl: Duration.seconds(1),
|
||||||
|
});
|
||||||
|
|
||||||
|
yield* TestClock.adjust(Duration.seconds(2));
|
||||||
|
|
||||||
|
const sessionError = yield* Effect.flip(sessions.verify(issued.token));
|
||||||
|
const websocketError = yield* Effect.flip(sessions.verifyWebSocketToken(websocket.token));
|
||||||
|
|
||||||
|
expect(sessionError._tag).toBe("SessionTokenExpiredError");
|
||||||
|
if (sessionError._tag === "SessionTokenExpiredError") {
|
||||||
|
expect(sessionError.sessionId).toBe(issued.sessionId);
|
||||||
|
expect(sessionError.expiresAt.epochMilliseconds).toBe(issued.expiresAt.epochMilliseconds);
|
||||||
|
expect(sessionError.observedAt.epochMilliseconds).toBeGreaterThan(
|
||||||
|
sessionError.expiresAt.epochMilliseconds,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
expect(websocketError._tag).toBe("WebSocketTokenExpiredError");
|
||||||
|
if (websocketError._tag === "WebSocketTokenExpiredError") {
|
||||||
|
expect(websocketError.sessionId).toBe(issued.sessionId);
|
||||||
|
expect(websocketError.expiresAt.epochMilliseconds).toBe(
|
||||||
|
websocket.expiresAt.epochMilliseconds,
|
||||||
|
);
|
||||||
|
expect(websocketError.observedAt.epochMilliseconds).toBeGreaterThan(
|
||||||
|
websocketError.expiresAt.epochMilliseconds,
|
||||||
|
);
|
||||||
|
}
|
||||||
}).pipe(Effect.provide(Layer.merge(makeSessionStoreLayer(), TestClock.layer()))),
|
}).pipe(Effect.provide(Layer.merge(makeSessionStoreLayer(), TestClock.layer()))),
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -173,12 +236,16 @@ it.layer(NodeServices.layer)("SessionStore.layer", (it) => {
|
|||||||
ipAddress: "192.168.1.88",
|
ipAddress: "192.168.1.88",
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
const clientWebSocket = yield* sessions.issueWebSocketToken(client.sessionId);
|
||||||
|
|
||||||
yield* sessions.markConnected(client.sessionId);
|
yield* sessions.markConnected(client.sessionId);
|
||||||
const beforeRevoke = yield* sessions.listActive();
|
const beforeRevoke = yield* sessions.listActive();
|
||||||
const revokedCount = yield* sessions.revokeAllExcept(administrative.sessionId);
|
const revokedCount = yield* sessions.revokeAllExcept(administrative.sessionId);
|
||||||
const afterRevoke = yield* sessions.listActive();
|
const afterRevoke = yield* sessions.listActive();
|
||||||
const revokedClient = yield* Effect.flip(sessions.verify(client.token));
|
const revokedClient = yield* Effect.flip(sessions.verify(client.token));
|
||||||
|
const revokedClientWebSocket = yield* Effect.flip(
|
||||||
|
sessions.verifyWebSocketToken(clientWebSocket.token),
|
||||||
|
);
|
||||||
|
|
||||||
expect(beforeRevoke).toHaveLength(2);
|
expect(beforeRevoke).toHaveLength(2);
|
||||||
expect(beforeRevoke.find((entry) => entry.sessionId === client.sessionId)?.connected).toBe(
|
expect(beforeRevoke.find((entry) => entry.sessionId === client.sessionId)?.connected).toBe(
|
||||||
@@ -194,7 +261,16 @@ it.layer(NodeServices.layer)("SessionStore.layer", (it) => {
|
|||||||
expect(revokedCount).toBe(1);
|
expect(revokedCount).toBe(1);
|
||||||
expect(afterRevoke).toHaveLength(1);
|
expect(afterRevoke).toHaveLength(1);
|
||||||
expect(afterRevoke[0]?.sessionId).toBe(administrative.sessionId);
|
expect(afterRevoke[0]?.sessionId).toBe(administrative.sessionId);
|
||||||
expect(revokedClient.message).toContain("revoked");
|
expect(revokedClient._tag).toBe("SessionTokenRevokedError");
|
||||||
|
if (revokedClient._tag === "SessionTokenRevokedError") {
|
||||||
|
expect(revokedClient.sessionId).toBe(client.sessionId);
|
||||||
|
expect(revokedClient.revokedAt.epochMilliseconds).toBeGreaterThanOrEqual(0);
|
||||||
|
}
|
||||||
|
expect(revokedClientWebSocket._tag).toBe("WebSocketSessionRevokedError");
|
||||||
|
if (revokedClientWebSocket._tag === "WebSocketSessionRevokedError") {
|
||||||
|
expect(revokedClientWebSocket.sessionId).toBe(client.sessionId);
|
||||||
|
expect(revokedClientWebSocket.revokedAt.epochMilliseconds).toBeGreaterThanOrEqual(0);
|
||||||
|
}
|
||||||
}).pipe(Effect.provide(makeSessionStoreLayer())),
|
}).pipe(Effect.provide(makeSessionStoreLayer())),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import {
|
|||||||
type AuthEnvironmentScope,
|
type AuthEnvironmentScope,
|
||||||
type ServerAuthSessionMethod,
|
type ServerAuthSessionMethod,
|
||||||
} from "@t3tools/contracts";
|
} from "@t3tools/contracts";
|
||||||
import * as Clock from "effect/Clock";
|
|
||||||
import * as Context from "effect/Context";
|
import * as Context from "effect/Context";
|
||||||
import * as Crypto from "effect/Crypto";
|
import * as Crypto from "effect/Crypto";
|
||||||
import * as DateTime from "effect/DateTime";
|
import * as DateTime from "effect/DateTime";
|
||||||
@@ -93,7 +92,11 @@ export class InvalidSessionTokenPayloadError extends Schema.TaggedErrorClass<Inv
|
|||||||
|
|
||||||
export class SessionTokenExpiredError extends Schema.TaggedErrorClass<SessionTokenExpiredError>()(
|
export class SessionTokenExpiredError extends Schema.TaggedErrorClass<SessionTokenExpiredError>()(
|
||||||
"SessionTokenExpiredError",
|
"SessionTokenExpiredError",
|
||||||
{},
|
{
|
||||||
|
sessionId: AuthSessionId,
|
||||||
|
expiresAt: Schema.DateTimeUtc,
|
||||||
|
observedAt: Schema.DateTimeUtc,
|
||||||
|
},
|
||||||
) {
|
) {
|
||||||
override get message(): string {
|
override get message(): string {
|
||||||
return "Session token expired.";
|
return "Session token expired.";
|
||||||
@@ -102,7 +105,9 @@ export class SessionTokenExpiredError extends Schema.TaggedErrorClass<SessionTok
|
|||||||
|
|
||||||
export class UnknownSessionTokenError extends Schema.TaggedErrorClass<UnknownSessionTokenError>()(
|
export class UnknownSessionTokenError extends Schema.TaggedErrorClass<UnknownSessionTokenError>()(
|
||||||
"UnknownSessionTokenError",
|
"UnknownSessionTokenError",
|
||||||
{},
|
{
|
||||||
|
sessionId: AuthSessionId,
|
||||||
|
},
|
||||||
) {
|
) {
|
||||||
override get message(): string {
|
override get message(): string {
|
||||||
return "Unknown session token.";
|
return "Unknown session token.";
|
||||||
@@ -111,7 +116,10 @@ export class UnknownSessionTokenError extends Schema.TaggedErrorClass<UnknownSes
|
|||||||
|
|
||||||
export class SessionTokenRevokedError extends Schema.TaggedErrorClass<SessionTokenRevokedError>()(
|
export class SessionTokenRevokedError extends Schema.TaggedErrorClass<SessionTokenRevokedError>()(
|
||||||
"SessionTokenRevokedError",
|
"SessionTokenRevokedError",
|
||||||
{},
|
{
|
||||||
|
sessionId: AuthSessionId,
|
||||||
|
revokedAt: Schema.DateTimeUtc,
|
||||||
|
},
|
||||||
) {
|
) {
|
||||||
override get message(): string {
|
override get message(): string {
|
||||||
return "Session token revoked.";
|
return "Session token revoked.";
|
||||||
@@ -120,7 +128,10 @@ export class SessionTokenRevokedError extends Schema.TaggedErrorClass<SessionTok
|
|||||||
|
|
||||||
export class InvalidSessionExpirationClaimError extends Schema.TaggedErrorClass<InvalidSessionExpirationClaimError>()(
|
export class InvalidSessionExpirationClaimError extends Schema.TaggedErrorClass<InvalidSessionExpirationClaimError>()(
|
||||||
"InvalidSessionExpirationClaimError",
|
"InvalidSessionExpirationClaimError",
|
||||||
{},
|
{
|
||||||
|
sessionId: AuthSessionId,
|
||||||
|
expirationClaim: Schema.Number,
|
||||||
|
},
|
||||||
) {
|
) {
|
||||||
override get message(): string {
|
override get message(): string {
|
||||||
return "Invalid `exp` claim";
|
return "Invalid `exp` claim";
|
||||||
@@ -158,7 +169,11 @@ export class InvalidWebSocketTokenPayloadError extends Schema.TaggedErrorClass<I
|
|||||||
|
|
||||||
export class WebSocketTokenExpiredError extends Schema.TaggedErrorClass<WebSocketTokenExpiredError>()(
|
export class WebSocketTokenExpiredError extends Schema.TaggedErrorClass<WebSocketTokenExpiredError>()(
|
||||||
"WebSocketTokenExpiredError",
|
"WebSocketTokenExpiredError",
|
||||||
{},
|
{
|
||||||
|
sessionId: AuthSessionId,
|
||||||
|
expiresAt: Schema.DateTimeUtc,
|
||||||
|
observedAt: Schema.DateTimeUtc,
|
||||||
|
},
|
||||||
) {
|
) {
|
||||||
override get message(): string {
|
override get message(): string {
|
||||||
return "Websocket token expired.";
|
return "Websocket token expired.";
|
||||||
@@ -167,7 +182,9 @@ export class WebSocketTokenExpiredError extends Schema.TaggedErrorClass<WebSocke
|
|||||||
|
|
||||||
export class UnknownWebSocketSessionError extends Schema.TaggedErrorClass<UnknownWebSocketSessionError>()(
|
export class UnknownWebSocketSessionError extends Schema.TaggedErrorClass<UnknownWebSocketSessionError>()(
|
||||||
"UnknownWebSocketSessionError",
|
"UnknownWebSocketSessionError",
|
||||||
{},
|
{
|
||||||
|
sessionId: AuthSessionId,
|
||||||
|
},
|
||||||
) {
|
) {
|
||||||
override get message(): string {
|
override get message(): string {
|
||||||
return "Unknown websocket session.";
|
return "Unknown websocket session.";
|
||||||
@@ -176,7 +193,11 @@ export class UnknownWebSocketSessionError extends Schema.TaggedErrorClass<Unknow
|
|||||||
|
|
||||||
export class WebSocketSessionExpiredError extends Schema.TaggedErrorClass<WebSocketSessionExpiredError>()(
|
export class WebSocketSessionExpiredError extends Schema.TaggedErrorClass<WebSocketSessionExpiredError>()(
|
||||||
"WebSocketSessionExpiredError",
|
"WebSocketSessionExpiredError",
|
||||||
{},
|
{
|
||||||
|
sessionId: AuthSessionId,
|
||||||
|
expiresAt: Schema.DateTimeUtc,
|
||||||
|
observedAt: Schema.DateTimeUtc,
|
||||||
|
},
|
||||||
) {
|
) {
|
||||||
override get message(): string {
|
override get message(): string {
|
||||||
return "Websocket session expired.";
|
return "Websocket session expired.";
|
||||||
@@ -185,7 +206,10 @@ export class WebSocketSessionExpiredError extends Schema.TaggedErrorClass<WebSoc
|
|||||||
|
|
||||||
export class WebSocketSessionRevokedError extends Schema.TaggedErrorClass<WebSocketSessionRevokedError>()(
|
export class WebSocketSessionRevokedError extends Schema.TaggedErrorClass<WebSocketSessionRevokedError>()(
|
||||||
"WebSocketSessionRevokedError",
|
"WebSocketSessionRevokedError",
|
||||||
{},
|
{
|
||||||
|
sessionId: AuthSessionId,
|
||||||
|
revokedAt: Schema.DateTimeUtc,
|
||||||
|
},
|
||||||
) {
|
) {
|
||||||
override get message(): string {
|
override get message(): string {
|
||||||
return "Websocket session revoked.";
|
return "Websocket session revoked.";
|
||||||
@@ -218,6 +242,7 @@ const sessionCredentialInternalErrorContext = {
|
|||||||
export class SessionClaimsEncodingError extends Schema.TaggedErrorClass<SessionClaimsEncodingError>()(
|
export class SessionClaimsEncodingError extends Schema.TaggedErrorClass<SessionClaimsEncodingError>()(
|
||||||
"SessionClaimsEncodingError",
|
"SessionClaimsEncodingError",
|
||||||
{
|
{
|
||||||
|
sessionId: AuthSessionId,
|
||||||
operation: Schema.Literals(["encode_session_claims", "encode_websocket_claims"]),
|
operation: Schema.Literals(["encode_session_claims", "encode_websocket_claims"]),
|
||||||
...sessionCredentialInternalErrorContext,
|
...sessionCredentialInternalErrorContext,
|
||||||
},
|
},
|
||||||
@@ -230,6 +255,7 @@ export class SessionClaimsEncodingError extends Schema.TaggedErrorClass<SessionC
|
|||||||
export class SessionCredentialIssueError extends Schema.TaggedErrorClass<SessionCredentialIssueError>()(
|
export class SessionCredentialIssueError extends Schema.TaggedErrorClass<SessionCredentialIssueError>()(
|
||||||
"SessionCredentialIssueError",
|
"SessionCredentialIssueError",
|
||||||
{
|
{
|
||||||
|
sessionId: Schema.optional(AuthSessionId),
|
||||||
...sessionCredentialInternalErrorContext,
|
...sessionCredentialInternalErrorContext,
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
@@ -241,6 +267,7 @@ export class SessionCredentialIssueError extends Schema.TaggedErrorClass<Session
|
|||||||
export class SessionCredentialVerificationError extends Schema.TaggedErrorClass<SessionCredentialVerificationError>()(
|
export class SessionCredentialVerificationError extends Schema.TaggedErrorClass<SessionCredentialVerificationError>()(
|
||||||
"SessionCredentialVerificationError",
|
"SessionCredentialVerificationError",
|
||||||
{
|
{
|
||||||
|
sessionId: AuthSessionId,
|
||||||
...sessionCredentialInternalErrorContext,
|
...sessionCredentialInternalErrorContext,
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
@@ -252,6 +279,7 @@ export class SessionCredentialVerificationError extends Schema.TaggedErrorClass<
|
|||||||
export class WebSocketTokenIssueError extends Schema.TaggedErrorClass<WebSocketTokenIssueError>()(
|
export class WebSocketTokenIssueError extends Schema.TaggedErrorClass<WebSocketTokenIssueError>()(
|
||||||
"WebSocketTokenIssueError",
|
"WebSocketTokenIssueError",
|
||||||
{
|
{
|
||||||
|
sessionId: AuthSessionId,
|
||||||
...sessionCredentialInternalErrorContext,
|
...sessionCredentialInternalErrorContext,
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
@@ -263,6 +291,7 @@ export class WebSocketTokenIssueError extends Schema.TaggedErrorClass<WebSocketT
|
|||||||
export class WebSocketTokenVerificationError extends Schema.TaggedErrorClass<WebSocketTokenVerificationError>()(
|
export class WebSocketTokenVerificationError extends Schema.TaggedErrorClass<WebSocketTokenVerificationError>()(
|
||||||
"WebSocketTokenVerificationError",
|
"WebSocketTokenVerificationError",
|
||||||
{
|
{
|
||||||
|
sessionId: AuthSessionId,
|
||||||
...sessionCredentialInternalErrorContext,
|
...sessionCredentialInternalErrorContext,
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
@@ -285,6 +314,7 @@ export class ActiveSessionsListError extends Schema.TaggedErrorClass<ActiveSessi
|
|||||||
export class SessionRevocationError extends Schema.TaggedErrorClass<SessionRevocationError>()(
|
export class SessionRevocationError extends Schema.TaggedErrorClass<SessionRevocationError>()(
|
||||||
"SessionRevocationError",
|
"SessionRevocationError",
|
||||||
{
|
{
|
||||||
|
sessionId: AuthSessionId,
|
||||||
...sessionCredentialInternalErrorContext,
|
...sessionCredentialInternalErrorContext,
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
@@ -296,6 +326,7 @@ export class SessionRevocationError extends Schema.TaggedErrorClass<SessionRevoc
|
|||||||
export class OtherSessionsRevocationError extends Schema.TaggedErrorClass<OtherSessionsRevocationError>()(
|
export class OtherSessionsRevocationError extends Schema.TaggedErrorClass<OtherSessionsRevocationError>()(
|
||||||
"OtherSessionsRevocationError",
|
"OtherSessionsRevocationError",
|
||||||
{
|
{
|
||||||
|
currentSessionId: AuthSessionId,
|
||||||
...sessionCredentialInternalErrorContext,
|
...sessionCredentialInternalErrorContext,
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
@@ -539,7 +570,11 @@ export const make = Effect.gen(function* () {
|
|||||||
const encodeClaims = Schema.encodeEffect(Schema.fromJsonString(SessionClaims));
|
const encodeClaims = Schema.encodeEffect(Schema.fromJsonString(SessionClaims));
|
||||||
const issue: SessionStore["Service"]["issue"] = Effect.fn("SessionStore.issue")(
|
const issue: SessionStore["Service"]["issue"] = Effect.fn("SessionStore.issue")(
|
||||||
function* (input) {
|
function* (input) {
|
||||||
const sessionId = AuthSessionId.make(yield* crypto.randomUUIDv4);
|
const sessionId = AuthSessionId.make(
|
||||||
|
yield* crypto.randomUUIDv4.pipe(
|
||||||
|
Effect.mapError((cause) => new SessionCredentialIssueError({ cause })),
|
||||||
|
),
|
||||||
|
);
|
||||||
const issuedAt = yield* DateTime.now;
|
const issuedAt = yield* DateTime.now;
|
||||||
const expiresAt = DateTime.add(issuedAt, {
|
const expiresAt = DateTime.add(issuedAt, {
|
||||||
milliseconds: Duration.toMillis(input?.ttl ?? DEFAULT_SESSION_TTL),
|
milliseconds: Duration.toMillis(input?.ttl ?? DEFAULT_SESSION_TTL),
|
||||||
@@ -559,27 +594,37 @@ export const make = Effect.gen(function* () {
|
|||||||
const encodedPayload = yield* encodeClaims(claims).pipe(
|
const encodedPayload = yield* encodeClaims(claims).pipe(
|
||||||
Effect.map(base64UrlEncode),
|
Effect.map(base64UrlEncode),
|
||||||
Effect.mapError(
|
Effect.mapError(
|
||||||
(cause) => new SessionClaimsEncodingError({ operation: "encode_session_claims", cause }),
|
(cause) =>
|
||||||
|
new SessionCredentialIssueError({
|
||||||
|
sessionId,
|
||||||
|
cause: new SessionClaimsEncodingError({
|
||||||
|
sessionId,
|
||||||
|
operation: "encode_session_claims",
|
||||||
|
cause,
|
||||||
|
}),
|
||||||
|
}),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
const signature = signPayload(encodedPayload, signingSecret);
|
const signature = signPayload(encodedPayload, signingSecret);
|
||||||
const client = input?.client ?? createDefaultClientMetadata();
|
const client = input?.client ?? createDefaultClientMetadata();
|
||||||
yield* authSessions.create({
|
yield* authSessions
|
||||||
sessionId,
|
.create({
|
||||||
subject: claims.sub,
|
sessionId,
|
||||||
scopes: claims.scopes,
|
subject: claims.sub,
|
||||||
method: claims.method,
|
scopes: claims.scopes,
|
||||||
client: {
|
method: claims.method,
|
||||||
label: client.label ?? null,
|
client: {
|
||||||
ipAddress: client.ipAddress ?? null,
|
label: client.label ?? null,
|
||||||
userAgent: client.userAgent ?? null,
|
ipAddress: client.ipAddress ?? null,
|
||||||
deviceType: client.deviceType,
|
userAgent: client.userAgent ?? null,
|
||||||
os: client.os ?? null,
|
deviceType: client.deviceType,
|
||||||
browser: client.browser ?? null,
|
os: client.os ?? null,
|
||||||
},
|
browser: client.browser ?? null,
|
||||||
issuedAt,
|
},
|
||||||
expiresAt,
|
issuedAt,
|
||||||
});
|
expiresAt,
|
||||||
|
})
|
||||||
|
.pipe(Effect.mapError((cause) => new SessionCredentialIssueError({ sessionId, cause })));
|
||||||
yield* emitUpsert(
|
yield* emitUpsert(
|
||||||
toAuthClientSession({
|
toAuthClientSession({
|
||||||
sessionId,
|
sessionId,
|
||||||
@@ -604,7 +649,6 @@ export const make = Effect.gen(function* () {
|
|||||||
...(claims.jkt ? { proofKeyThumbprint: claims.jkt } : {}),
|
...(claims.jkt ? { proofKeyThumbprint: claims.jkt } : {}),
|
||||||
} satisfies IssuedSession;
|
} satisfies IssuedSession;
|
||||||
},
|
},
|
||||||
Effect.mapError((cause) => new SessionCredentialIssueError({ cause })),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
const verify: SessionStore["Service"]["verify"] = Effect.fn("SessionStore.verify")(
|
const verify: SessionStore["Service"]["verify"] = Effect.fn("SessionStore.verify")(
|
||||||
@@ -623,22 +667,37 @@ export const make = Effect.gen(function* () {
|
|||||||
Effect.mapError((cause) => new InvalidSessionTokenPayloadError({ cause })),
|
Effect.mapError((cause) => new InvalidSessionTokenPayloadError({ cause })),
|
||||||
);
|
);
|
||||||
|
|
||||||
const now = yield* Clock.currentTimeMillis;
|
const observedAt = yield* DateTime.now;
|
||||||
if (claims.exp <= now) {
|
|
||||||
return yield* new SessionTokenExpiredError({});
|
|
||||||
}
|
|
||||||
|
|
||||||
const row = yield* authSessions.getById({ sessionId: claims.sid });
|
|
||||||
if (Option.isNone(row)) {
|
|
||||||
return yield* new UnknownSessionTokenError({});
|
|
||||||
}
|
|
||||||
if (row.value.revokedAt !== null) {
|
|
||||||
return yield* new SessionTokenRevokedError({});
|
|
||||||
}
|
|
||||||
|
|
||||||
const expiresAt = DateTime.make(claims.exp);
|
const expiresAt = DateTime.make(claims.exp);
|
||||||
if (Option.isNone(expiresAt)) {
|
if (Option.isNone(expiresAt)) {
|
||||||
return yield* new InvalidSessionExpirationClaimError({});
|
return yield* new InvalidSessionExpirationClaimError({
|
||||||
|
sessionId: claims.sid,
|
||||||
|
expirationClaim: claims.exp,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (claims.exp <= observedAt.epochMilliseconds) {
|
||||||
|
return yield* new SessionTokenExpiredError({
|
||||||
|
sessionId: claims.sid,
|
||||||
|
expiresAt: expiresAt.value,
|
||||||
|
observedAt,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const row = yield* authSessions
|
||||||
|
.getById({ sessionId: claims.sid })
|
||||||
|
.pipe(
|
||||||
|
Effect.mapError(
|
||||||
|
(cause) => new SessionCredentialVerificationError({ sessionId: claims.sid, cause }),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
if (Option.isNone(row)) {
|
||||||
|
return yield* new UnknownSessionTokenError({ sessionId: claims.sid });
|
||||||
|
}
|
||||||
|
if (row.value.revokedAt !== null) {
|
||||||
|
return yield* new SessionTokenRevokedError({
|
||||||
|
sessionId: claims.sid,
|
||||||
|
revokedAt: row.value.revokedAt,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -652,95 +711,111 @@ export const make = Effect.gen(function* () {
|
|||||||
...(claims.jkt ? { proofKeyThumbprint: claims.jkt } : {}),
|
...(claims.jkt ? { proofKeyThumbprint: claims.jkt } : {}),
|
||||||
} satisfies VerifiedSession;
|
} satisfies VerifiedSession;
|
||||||
},
|
},
|
||||||
Effect.mapError((cause) =>
|
|
||||||
isSessionCredentialInvalidError(cause)
|
|
||||||
? cause
|
|
||||||
: new SessionCredentialVerificationError({ cause }),
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
const encodeWsClaims = Schema.encodeEffect(Schema.fromJsonString(WebSocketClaims));
|
const encodeWsClaims = Schema.encodeEffect(Schema.fromJsonString(WebSocketClaims));
|
||||||
const issueWebSocketToken: SessionStore["Service"]["issueWebSocketToken"] = Effect.fn(
|
const issueWebSocketToken: SessionStore["Service"]["issueWebSocketToken"] = Effect.fn(
|
||||||
"SessionStore.issueWebSocketToken",
|
"SessionStore.issueWebSocketToken",
|
||||||
)(
|
)(function* (sessionId, input) {
|
||||||
function* (sessionId, input) {
|
const issuedAt = yield* DateTime.now;
|
||||||
const issuedAt = yield* DateTime.now;
|
const expiresAt = DateTime.add(issuedAt, {
|
||||||
const expiresAt = DateTime.add(issuedAt, {
|
milliseconds: Duration.toMillis(input?.ttl ?? DEFAULT_WEBSOCKET_TOKEN_TTL),
|
||||||
milliseconds: Duration.toMillis(input?.ttl ?? DEFAULT_WEBSOCKET_TOKEN_TTL),
|
});
|
||||||
});
|
const claims: WebSocketClaims = {
|
||||||
const claims: WebSocketClaims = {
|
v: 1,
|
||||||
v: 1,
|
kind: "websocket",
|
||||||
kind: "websocket",
|
sid: sessionId,
|
||||||
sid: sessionId,
|
iat: issuedAt.epochMilliseconds,
|
||||||
iat: issuedAt.epochMilliseconds,
|
exp: expiresAt.epochMilliseconds,
|
||||||
exp: expiresAt.epochMilliseconds,
|
};
|
||||||
};
|
const encodedPayload = yield* encodeWsClaims(claims).pipe(
|
||||||
const encodedPayload = yield* encodeWsClaims(claims).pipe(
|
Effect.map(base64UrlEncode),
|
||||||
Effect.map(base64UrlEncode),
|
Effect.mapError(
|
||||||
Effect.mapError(
|
(cause) =>
|
||||||
(cause) =>
|
new WebSocketTokenIssueError({
|
||||||
new SessionClaimsEncodingError({ operation: "encode_websocket_claims", cause }),
|
sessionId,
|
||||||
),
|
cause: new SessionClaimsEncodingError({
|
||||||
);
|
sessionId,
|
||||||
const signature = signPayload(encodedPayload, signingSecret);
|
operation: "encode_websocket_claims",
|
||||||
return {
|
cause,
|
||||||
token: `${encodedPayload}.${signature}`,
|
}),
|
||||||
expiresAt,
|
}),
|
||||||
};
|
),
|
||||||
},
|
);
|
||||||
Effect.mapError((cause) => new WebSocketTokenIssueError({ cause })),
|
const signature = signPayload(encodedPayload, signingSecret);
|
||||||
);
|
return {
|
||||||
|
token: `${encodedPayload}.${signature}`,
|
||||||
|
expiresAt,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
const verifyWebSocketToken: SessionStore["Service"]["verifyWebSocketToken"] = Effect.fn(
|
const verifyWebSocketToken: SessionStore["Service"]["verifyWebSocketToken"] = Effect.fn(
|
||||||
"SessionStore.verifyWebSocketToken",
|
"SessionStore.verifyWebSocketToken",
|
||||||
)(
|
)(function* (token) {
|
||||||
function* (token) {
|
const [encodedPayload, signature] = token.split(".");
|
||||||
const [encodedPayload, signature] = token.split(".");
|
if (!encodedPayload || !signature) {
|
||||||
if (!encodedPayload || !signature) {
|
return yield* new MalformedWebSocketTokenError({});
|
||||||
return yield* new MalformedWebSocketTokenError({});
|
}
|
||||||
}
|
|
||||||
|
|
||||||
const expectedSignature = signPayload(encodedPayload, signingSecret);
|
const expectedSignature = signPayload(encodedPayload, signingSecret);
|
||||||
if (!timingSafeEqualBase64Url(signature, expectedSignature)) {
|
if (!timingSafeEqualBase64Url(signature, expectedSignature)) {
|
||||||
return yield* new InvalidWebSocketTokenSignatureError({});
|
return yield* new InvalidWebSocketTokenSignatureError({});
|
||||||
}
|
}
|
||||||
|
|
||||||
const claims = yield* decodeWebSocketClaims(base64UrlDecodeUtf8(encodedPayload)).pipe(
|
const claims = yield* decodeWebSocketClaims(base64UrlDecodeUtf8(encodedPayload)).pipe(
|
||||||
Effect.mapError((cause) => new InvalidWebSocketTokenPayloadError({ cause })),
|
Effect.mapError((cause) => new InvalidWebSocketTokenPayloadError({ cause })),
|
||||||
|
);
|
||||||
|
|
||||||
|
const observedAt = yield* DateTime.now;
|
||||||
|
const expiresAt = DateTime.make(claims.exp);
|
||||||
|
if (Option.isNone(expiresAt)) {
|
||||||
|
return yield* new InvalidSessionExpirationClaimError({
|
||||||
|
sessionId: claims.sid,
|
||||||
|
expirationClaim: claims.exp,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (claims.exp <= observedAt.epochMilliseconds) {
|
||||||
|
return yield* new WebSocketTokenExpiredError({
|
||||||
|
sessionId: claims.sid,
|
||||||
|
expiresAt: expiresAt.value,
|
||||||
|
observedAt,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const row = yield* authSessions
|
||||||
|
.getById({ sessionId: claims.sid })
|
||||||
|
.pipe(
|
||||||
|
Effect.mapError(
|
||||||
|
(cause) => new WebSocketTokenVerificationError({ sessionId: claims.sid, cause }),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
|
if (Option.isNone(row)) {
|
||||||
const now = yield* Clock.currentTimeMillis;
|
return yield* new UnknownWebSocketSessionError({ sessionId: claims.sid });
|
||||||
if (claims.exp <= now) {
|
}
|
||||||
return yield* new WebSocketTokenExpiredError({});
|
if (row.value.expiresAt.epochMilliseconds <= observedAt.epochMilliseconds) {
|
||||||
}
|
return yield* new WebSocketSessionExpiredError({
|
||||||
|
sessionId: claims.sid,
|
||||||
const row = yield* authSessions.getById({ sessionId: claims.sid });
|
|
||||||
if (Option.isNone(row)) {
|
|
||||||
return yield* new UnknownWebSocketSessionError({});
|
|
||||||
}
|
|
||||||
if (row.value.expiresAt.epochMilliseconds <= now) {
|
|
||||||
return yield* new WebSocketSessionExpiredError({});
|
|
||||||
}
|
|
||||||
if (row.value.revokedAt !== null) {
|
|
||||||
return yield* new WebSocketSessionRevokedError({});
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
sessionId: row.value.sessionId,
|
|
||||||
token,
|
|
||||||
method: row.value.method,
|
|
||||||
client: toClientMetadata(row.value.client),
|
|
||||||
expiresAt: row.value.expiresAt,
|
expiresAt: row.value.expiresAt,
|
||||||
subject: row.value.subject,
|
observedAt,
|
||||||
scopes: row.value.scopes,
|
});
|
||||||
} satisfies VerifiedSession;
|
}
|
||||||
},
|
if (row.value.revokedAt !== null) {
|
||||||
Effect.mapError((cause) =>
|
return yield* new WebSocketSessionRevokedError({
|
||||||
isSessionCredentialInvalidError(cause)
|
sessionId: claims.sid,
|
||||||
? cause
|
revokedAt: row.value.revokedAt,
|
||||||
: new WebSocketTokenVerificationError({ cause }),
|
});
|
||||||
),
|
}
|
||||||
);
|
|
||||||
|
return {
|
||||||
|
sessionId: row.value.sessionId,
|
||||||
|
token,
|
||||||
|
method: row.value.method,
|
||||||
|
client: toClientMetadata(row.value.client),
|
||||||
|
expiresAt: row.value.expiresAt,
|
||||||
|
subject: row.value.subject,
|
||||||
|
scopes: row.value.scopes,
|
||||||
|
} satisfies VerifiedSession;
|
||||||
|
});
|
||||||
|
|
||||||
const listActive: SessionStore["Service"]["listActive"] = Effect.fn("SessionStore.listActive")(
|
const listActive: SessionStore["Service"]["listActive"] = Effect.fn("SessionStore.listActive")(
|
||||||
function* () {
|
function* () {
|
||||||
@@ -768,10 +843,12 @@ export const make = Effect.gen(function* () {
|
|||||||
const revoke: SessionStore["Service"]["revoke"] = Effect.fn("SessionStore.revoke")(
|
const revoke: SessionStore["Service"]["revoke"] = Effect.fn("SessionStore.revoke")(
|
||||||
function* (sessionId) {
|
function* (sessionId) {
|
||||||
const revokedAt = yield* DateTime.now;
|
const revokedAt = yield* DateTime.now;
|
||||||
const revoked = yield* authSessions.revoke({
|
const revoked = yield* authSessions
|
||||||
sessionId,
|
.revoke({
|
||||||
revokedAt,
|
sessionId,
|
||||||
});
|
revokedAt,
|
||||||
|
})
|
||||||
|
.pipe(Effect.mapError((cause) => new SessionRevocationError({ sessionId, cause })));
|
||||||
if (revoked) {
|
if (revoked) {
|
||||||
yield* Ref.update(connectedSessionsRef, (current) => {
|
yield* Ref.update(connectedSessionsRef, (current) => {
|
||||||
const next = new Map(current);
|
const next = new Map(current);
|
||||||
@@ -782,39 +859,41 @@ export const make = Effect.gen(function* () {
|
|||||||
}
|
}
|
||||||
return revoked;
|
return revoked;
|
||||||
},
|
},
|
||||||
Effect.mapError((cause) => new SessionRevocationError({ cause })),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
const revokeAllExcept: SessionStore["Service"]["revokeAllExcept"] = Effect.fn(
|
const revokeAllExcept: SessionStore["Service"]["revokeAllExcept"] = Effect.fn(
|
||||||
"SessionStore.revokeAllExcept",
|
"SessionStore.revokeAllExcept",
|
||||||
)(
|
)(function* (sessionId) {
|
||||||
function* (sessionId) {
|
const revokedAt = yield* DateTime.now;
|
||||||
const revokedAt = yield* DateTime.now;
|
const revokedSessionIds = yield* authSessions
|
||||||
const revokedSessionIds = yield* authSessions.revokeAllExcept({
|
.revokeAllExcept({
|
||||||
currentSessionId: sessionId,
|
currentSessionId: sessionId,
|
||||||
revokedAt,
|
revokedAt,
|
||||||
|
})
|
||||||
|
.pipe(
|
||||||
|
Effect.mapError(
|
||||||
|
(cause) => new OtherSessionsRevocationError({ currentSessionId: sessionId, cause }),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
if (revokedSessionIds.length > 0) {
|
||||||
|
yield* Ref.update(connectedSessionsRef, (current) => {
|
||||||
|
const next = new Map(current);
|
||||||
|
for (const revokedSessionId of revokedSessionIds) {
|
||||||
|
next.delete(revokedSessionId);
|
||||||
|
}
|
||||||
|
return next;
|
||||||
});
|
});
|
||||||
if (revokedSessionIds.length > 0) {
|
yield* Effect.forEach(
|
||||||
yield* Ref.update(connectedSessionsRef, (current) => {
|
revokedSessionIds,
|
||||||
const next = new Map(current);
|
(revokedSessionId) => emitRemoved(revokedSessionId),
|
||||||
for (const revokedSessionId of revokedSessionIds) {
|
{
|
||||||
next.delete(revokedSessionId);
|
concurrency: "unbounded",
|
||||||
}
|
discard: true,
|
||||||
return next;
|
},
|
||||||
});
|
);
|
||||||
yield* Effect.forEach(
|
}
|
||||||
revokedSessionIds,
|
return revokedSessionIds.length;
|
||||||
(revokedSessionId) => emitRemoved(revokedSessionId),
|
});
|
||||||
{
|
|
||||||
concurrency: "unbounded",
|
|
||||||
discard: true,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return revokedSessionIds.length;
|
|
||||||
},
|
|
||||||
Effect.mapError((cause) => new OtherSessionsRevocationError({ cause })),
|
|
||||||
);
|
|
||||||
|
|
||||||
return SessionStore.of({
|
return SessionStore.of({
|
||||||
cookieName,
|
cookieName,
|
||||||
|
|||||||
Reference in new issue
Block a user