| 1 | import { describe, expect, it } from 'vitest';
|
| 2 | import {
|
| 3 | agentAccessTokenClientId,
|
| 4 | agentAccessTokenId,
|
| 5 | createAgentAccessToken,
|
| 6 | matchesAgentAccessToken,
|
| 7 | } from '../packages/persistence/src/access-token.js';
|
| 8 |
|
| 9 | describe('agent access tokens', () => {
|
| 10 | it('creates opaque bearer credentials whose secret is never needed for lookup', () => {
|
| 11 | const created = createAgentAccessToken();
|
| 12 | expect(created.token).toMatch(/^oc_at_[A-Za-z0-9_-]{16}\.[A-Za-z0-9_-]{43}$/);
|
| 13 | expect(agentAccessTokenId(created.token)).toBe(created.tokenId);
|
| 14 | expect(created.tokenHash).toMatch(/^[a-f0-9]{64}$/);
|
| 15 | expect(matchesAgentAccessToken(created.token, created.tokenHash)).toBe(true);
|
| 16 | expect(agentAccessTokenClientId(created.tokenId)).toBe(
|
| 17 | `openclasp-agent-token:${created.tokenId}`,
|
| 18 | );
|
| 19 | });
|
| 20 |
|
| 21 | it('rejects malformed and tampered credentials', () => {
|
| 22 | const created = createAgentAccessToken();
|
| 23 | expect(agentAccessTokenId('not-a-token')).toBeUndefined();
|
| 24 | expect(matchesAgentAccessToken(`${created.token}x`, created.tokenHash)).toBe(false);
|
| 25 | expect(matchesAgentAccessToken(created.token, '0'.repeat(64))).toBe(false);
|
| 26 | });
|
| 27 | });
|