42 lines
2.7 KiB
TypeScript
42 lines
2.7 KiB
TypeScript
import { ESI_WALLET_SCOPE } from '../../../src/lib/server/esi';
|
|
import type { AuthorizationKey } from '../../../src/lib/server/esi';
|
|
import type { ResourcePrincipal, SelectedResource } from '../../../src/lib/domain/model';
|
|
import type {
|
|
WalletAuthorizedResource,
|
|
WalletAuthorityFence,
|
|
WalletBalanceSnapshot,
|
|
WalletCredential,
|
|
} from '../../../src/lib/server/wallet/balance-repository';
|
|
|
|
export function resource(userId: string, characterId: number, overrides: Partial<WalletAuthorizedResource> = {}): WalletAuthorizedResource {
|
|
const principal: Extract<ResourcePrincipal, { kind: 'character' }> = { kind: 'character', id: String(characterId), ownerUserId: userId };
|
|
const selected: SelectedResource = { principal, requiredScopes: [ESI_WALLET_SCOPE], authorizationRevision: overrides.grantRevision ?? 1, authorizationEpoch: overrides.authorizationEpoch ?? 1 };
|
|
return {
|
|
userId,
|
|
characterId,
|
|
principal: overrides.principal ?? principal,
|
|
resource: overrides.resource ?? selected,
|
|
authorizationStatus: 'active',
|
|
consent: 'granted',
|
|
grantedScopes: [ESI_WALLET_SCOPE],
|
|
grantRevision: 1,
|
|
authorizationEpoch: 1,
|
|
credentialVersion: 1,
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
export function fence(value: WalletAuthorizedResource): WalletAuthorityFence {
|
|
return { userId: value.userId, characterId: value.characterId, principal: { ...value.principal }, grantRevision: value.grantRevision, authorizationEpoch: value.authorizationEpoch, credentialVersion: value.credentialVersion!, scopes: [...value.grantedScopes] };
|
|
}
|
|
|
|
export function snapshot(value: WalletAuthorizedResource, amount: string, fetchedAt = 100, expiresAt = 1_000, generation = `wallet-${value.characterId}-${fetchedAt}`): WalletBalanceSnapshot {
|
|
return { userId: value.userId, characterId: value.characterId, principal: { ...value.principal }, amount, fetchedAt, expiresAt, upstreamEffectiveAt: null, coverage: { kind: 'snapshot', generation, complete: true }, generation, authorizationRevision: value.grantRevision, authorizationEpoch: value.authorizationEpoch, credentialVersion: value.credentialVersion!, };
|
|
}
|
|
|
|
export function credential(value: WalletAuthorizedResource, token = `token-${value.userId}-${value.characterId}`): { fence: WalletAuthorityFence; credential: WalletCredential } {
|
|
const current = fence(value);
|
|
const authorizationKey: AuthorizationKey = { userId: value.userId, principal: `character:${value.principal.id}`, characterId: value.characterId, scopes: [...value.grantedScopes], grantRevision: value.grantRevision, authorizationEpoch: value.authorizationEpoch, credentialVersion: value.credentialVersion! };
|
|
return { fence: current, credential: { accessToken: token, authorizationKey } };
|
|
}
|