@oa2/core
Version:
A comprehensive, RFC-compliant OAuth 2.0 authorization server implementation in TypeScript
1 lines • 6.25 kB
Source Map (JSON)
{"version":3,"file":"index.d.ts","sources":["../../src/testing/storage.ts"],"sourcesContent":["import { Client, Token, AuthorizationCode, StorageAdapter } from '../../src/types';\n\n/**\n * An in-memory implementation of the StorageAdapter interface for testing purposes.\n * This adapter stores clients, tokens, authorization codes, and users in JavaScript Maps.\n */\nexport class InMemoryStorageAdapter implements StorageAdapter {\n private clients: Map<string, Client> = new Map();\n private tokens: Map<string, Token> = new Map();\n private authorizationCodes: Map<string, AuthorizationCode> = new Map();\n private users: Map<string, any> = new Map();\n\n constructor() {\n // Add a default client for testing\n this.clients.set('test_client', {\n id: 'test_client',\n secret: 'test_secret',\n redirectUris: ['https://client.example.com/cb'],\n allowedGrants: ['authorization_code', 'client_credentials', 'refresh_token'],\n scopes: ['read', 'write', 'offline_access'],\n });\n this.users.set('test_user', { id: 'test_user', username: 'testuser', password: 'password' });\n }\n\n getUserByCredentials(username: string, password: string): Promise<any | null> {\n const user = Array.from(this.users.values()).find(\n (user) => user.username === username && user.password === password,\n );\n return Promise.resolve(user || null);\n }\n\n /**\n * Retrieves a client by its ID from memory.\n * @param clientId The ID of the client.\n * @returns A Promise that resolves to the Client object or null if not found.\n * @see RFC 6749, Section 2.2 Client Identifier\n */\n async getClient(clientId: string): Promise<Client | null> {\n return this.clients.get(clientId) || null;\n }\n\n /**\n * Saves a token (access token and/or refresh token) to memory.\n * @param token The Token object to save.\n * @returns A Promise that resolves when the token is saved.\n * @see RFC 6749, Section 1.4 Access Token\n * @see RFC 6749, Section 1.5 Refresh Token\n */\n async saveToken(token: Token): Promise<void> {\n this.tokens.set(token.accessToken, token);\n if (token.refreshToken) {\n this.tokens.set(token.refreshToken, token);\n }\n }\n\n /**\n * Retrieves an access token from memory.\n * @param accessToken The access token string.\n * @returns A Promise that resolves to the Token object or null if not found.\n * @see RFC 6749, Section 1.4 Access Token\n */\n async getAccessToken(accessToken: string): Promise<Token | null> {\n const token = this.tokens.get(accessToken);\n if (token && token.accessToken === accessToken) {\n return token;\n }\n return null;\n }\n\n /**\n * Retrieves a refresh token from memory.\n * @param refreshToken The refresh token string.\n * @returns A Promise that resolves to the Token object or null if not found.\n * @see RFC 6749, Section 1.5 Refresh Token\n */\n async getRefreshToken(refreshToken: string): Promise<Token | null> {\n const token = this.tokens.get(refreshToken);\n if (token && token.refreshToken === refreshToken) {\n return token;\n }\n return null;\n }\n\n /**\n * Saves an authorization code to memory.\n * @param code The AuthorizationCode object to save.\n * @returns A Promise that resolves when the authorization code is saved.\n * @see RFC 6749, Section 1.3.1 Authorization Code\n */\n async saveAuthorizationCode(code: AuthorizationCode): Promise<void> {\n this.authorizationCodes.set(code.code, code);\n }\n\n /**\n * Retrieves an authorization code from memory.\n * @param code The authorization code string.\n * @returns A Promise that resolves to the AuthorizationCode object or null if not found.\n * @see RFC 6749, Section 1.3.1 Authorization Code\n */\n async getAuthorizationCode(code: string): Promise<AuthorizationCode | null> {\n return this.authorizationCodes.get(code) || null;\n }\n\n /**\n * Deletes an authorization code from memory.\n * @param code The authorization code string to delete.\n * @returns A Promise that resolves when the authorization code is deleted.\n * @see RFC 6749, Section 4.1.2 Authorization Response\n * \"The client MUST NOT use the authorization code more than once.\"\n */\n async deleteAuthorizationCode(code: string): Promise<void> {\n this.authorizationCodes.delete(code);\n }\n\n /**\n * Revokes a token (access token or refresh token) from memory.\n * @param token The token string to revoke.\n * @returns A Promise that resolves when the token is revoked.\n * @see RFC 7009, OAuth 2.0 Token Revocation\n */\n async revokeToken(token: string): Promise<void> {\n // In a real implementation, you'd need to find all tokens associated with this token string\n // and invalidate them. For this in-memory mock, we'll just delete the entry if it exists.\n this.tokens.delete(token);\n }\n\n /**\n * Retrieves a user by their ID from memory.\n * @param userId The ID of the user.\n * @returns A Promise that resolves to the user object or null if not found.\n * @see RFC 6749, Section 1.1 Roles (Resource Owner)\n */\n async getUser(userId: string): Promise<any | null> {\n return this.users.get(userId) || null;\n }\n\n /**\n * Saves a client to memory.\n * @param client The Client object to save.\n * @returns A Promise that resolves when the client is saved.\n * @see RFC 6749, Section 2. Client Registration\n */\n async saveClient(client: Client): Promise<void> {\n this.clients.set(client.id, client);\n }\n}\n"],"names":[],"mappings":";;AACA;AACA;AACA;AACA;AACO,cAAA,sBAAA,YAAA,cAAA;AACP;AACA;AACA;AACA;AACA;AACA,8DAAA,OAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,iCAAA,OAAA,CAAA,MAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,qBAAA,KAAA,GAAA,OAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,yCAAA,OAAA,CAAA,KAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,2CAAA,OAAA,CAAA,KAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,gCAAA,iBAAA,GAAA,OAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,wCAAA,OAAA,CAAA,iBAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,2CAAA,OAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,gCAAA,OAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,6BAAA,OAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,uBAAA,MAAA,GAAA,OAAA;AACA;;;;"}