@authrim/core 0.1.8 → 0.1.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +188 -188
- package/dist/index.cjs +2 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +57 -1
- package/dist/index.d.ts +57 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,188 +1,188 @@
|
|
|
1
|
-
# @authrim/core
|
|
2
|
-
|
|
3
|
-
Platform-agnostic core library for [Authrim](https://github.com/authrim) - a modern, developer-friendly Identity Provider.
|
|
4
|
-
|
|
5
|
-
## Overview
|
|
6
|
-
|
|
7
|
-
This is the core package that provides OAuth 2.0 / OpenID Connect functionality without platform-specific dependencies. For most applications, use a framework-specific package (e.g., `@authrim/react`, `@authrim/web`) which includes this as a dependency.
|
|
8
|
-
|
|
9
|
-
**Use this package directly if you're:**
|
|
10
|
-
- Building a custom integration
|
|
11
|
-
- Creating a new platform-specific package
|
|
12
|
-
- Need full control over HTTP, crypto, and storage implementations
|
|
13
|
-
|
|
14
|
-
## Installation
|
|
15
|
-
|
|
16
|
-
```bash
|
|
17
|
-
npm install @authrim/core
|
|
18
|
-
```
|
|
19
|
-
|
|
20
|
-
## Features
|
|
21
|
-
|
|
22
|
-
- **Authorization Code Flow with PKCE** - RFC 7636 compliant
|
|
23
|
-
- **Automatic Token Refresh** - With request coalescing for concurrent calls
|
|
24
|
-
- **Token Exchange** - RFC 8693 for delegation and cross-service scenarios
|
|
25
|
-
- **Token Introspection** - RFC 7662 server-side validation
|
|
26
|
-
- **Token Revocation** - RFC 7009 explicit invalidation
|
|
27
|
-
- **Silent Authentication** - prompt=none foundation
|
|
28
|
-
- **RP-Initiated Logout** - OIDC logout with optional token revocation
|
|
29
|
-
- **OIDC Discovery** - Automatic endpoint discovery with caching
|
|
30
|
-
- **Event System** - Subscribe to authentication lifecycle events
|
|
31
|
-
- **Error Recovery Metadata** - Guidance for retry logic and user actions
|
|
32
|
-
|
|
33
|
-
## Usage
|
|
34
|
-
|
|
35
|
-
When using `@authrim/core` directly, you must provide platform implementations:
|
|
36
|
-
|
|
37
|
-
```typescript
|
|
38
|
-
import { createAuthrimClient } from '@authrim/core';
|
|
39
|
-
|
|
40
|
-
const client = await createAuthrimClient({
|
|
41
|
-
issuer: 'https://your-idp.com',
|
|
42
|
-
clientId: 'your-client-id',
|
|
43
|
-
crypto: yourCryptoProvider, // CryptoProvider interface
|
|
44
|
-
storage: yourStorageProvider, // AuthrimStorage interface
|
|
45
|
-
http: yourHttpProvider, // HttpClient interface
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
// Start login
|
|
49
|
-
const { url } = await client.buildAuthorizationUrl({
|
|
50
|
-
redirectUri: 'https://your-app.com/callback',
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
// Handle callback
|
|
54
|
-
const tokens = await client.handleCallback(callbackUrl);
|
|
55
|
-
|
|
56
|
-
// Use tokens
|
|
57
|
-
const accessToken = await client.token.getAccessToken();
|
|
58
|
-
```
|
|
59
|
-
|
|
60
|
-
## API Reference
|
|
61
|
-
|
|
62
|
-
### Token Operations
|
|
63
|
-
|
|
64
|
-
```typescript
|
|
65
|
-
// Get access token (auto-refreshes if needed)
|
|
66
|
-
const accessToken = await client.token.getAccessToken();
|
|
67
|
-
|
|
68
|
-
// Get all tokens
|
|
69
|
-
const tokens = await client.token.getTokens();
|
|
70
|
-
|
|
71
|
-
// Token Exchange (RFC 8693)
|
|
72
|
-
const result = await client.token.exchange({
|
|
73
|
-
subjectToken: currentToken,
|
|
74
|
-
audience: 'https://api.other-service.com',
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
// Token Introspection (RFC 7662)
|
|
78
|
-
const info = await client.token.introspect({ token: accessToken });
|
|
79
|
-
|
|
80
|
-
// Token Revocation (RFC 7009)
|
|
81
|
-
await client.token.revoke({ token: accessToken });
|
|
82
|
-
```
|
|
83
|
-
|
|
84
|
-
### Session & Logout
|
|
85
|
-
|
|
86
|
-
```typescript
|
|
87
|
-
// Check authentication status
|
|
88
|
-
const isAuth = await client.isAuthenticated();
|
|
89
|
-
|
|
90
|
-
// Get user info
|
|
91
|
-
const user = await client.getUser();
|
|
92
|
-
|
|
93
|
-
// Logout with token revocation
|
|
94
|
-
const { logoutUrl } = await client.logout({
|
|
95
|
-
revokeTokens: true,
|
|
96
|
-
postLogoutRedirectUri: 'https://your-app.com',
|
|
97
|
-
});
|
|
98
|
-
```
|
|
99
|
-
|
|
100
|
-
### Events
|
|
101
|
-
|
|
102
|
-
```typescript
|
|
103
|
-
client.on('token:refreshed', ({ tokens }) => {
|
|
104
|
-
console.log('Token refreshed');
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
client.on('token:error', ({ error }) => {
|
|
108
|
-
if (error.meta.retryable) {
|
|
109
|
-
// Retry after error.meta.retryAfterMs
|
|
110
|
-
} else if (error.meta.userAction === 'reauthenticate') {
|
|
111
|
-
// Redirect to login
|
|
112
|
-
}
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
client.on('session:ended', ({ reason }) => {
|
|
116
|
-
console.log('Session ended:', reason);
|
|
117
|
-
});
|
|
118
|
-
```
|
|
119
|
-
|
|
120
|
-
### Error Handling
|
|
121
|
-
|
|
122
|
-
All errors include recovery metadata:
|
|
123
|
-
|
|
124
|
-
```typescript
|
|
125
|
-
try {
|
|
126
|
-
await client.token.getAccessToken();
|
|
127
|
-
} catch (error) {
|
|
128
|
-
if (error instanceof AuthrimError) {
|
|
129
|
-
console.log('Error code:', error.code);
|
|
130
|
-
console.log('Retryable:', error.meta.retryable);
|
|
131
|
-
console.log('User action:', error.meta.userAction);
|
|
132
|
-
// 'retry' | 'reauthenticate' | 'contact_support' | 'check_network' | 'none'
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
```
|
|
136
|
-
|
|
137
|
-
## Provider Interfaces
|
|
138
|
-
|
|
139
|
-
Implement these interfaces for your platform:
|
|
140
|
-
|
|
141
|
-
```typescript
|
|
142
|
-
interface CryptoProvider {
|
|
143
|
-
randomBytes(length: number): Uint8Array;
|
|
144
|
-
sha256(data: string | Uint8Array): Promise<Uint8Array>;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
interface AuthrimStorage {
|
|
148
|
-
get(key: string): Promise<string | null>;
|
|
149
|
-
set(key: string, value: string): Promise<void>;
|
|
150
|
-
remove(key: string): Promise<void>;
|
|
151
|
-
getAll(): Promise<Record<string, string>>;
|
|
152
|
-
clear(): Promise<void>;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
interface HttpClient {
|
|
156
|
-
fetch<T>(url: string, options?: HttpOptions): Promise<HttpResponse<T>>;
|
|
157
|
-
}
|
|
158
|
-
```
|
|
159
|
-
|
|
160
|
-
## Standards Compliance
|
|
161
|
-
|
|
162
|
-
| Standard | Status |
|
|
163
|
-
|----------|--------|
|
|
164
|
-
| OAuth 2.0 (RFC 6749) | Authorization Code Flow |
|
|
165
|
-
| PKCE (RFC 7636) | S256 method |
|
|
166
|
-
| Token Introspection (RFC 7662) | Full support |
|
|
167
|
-
| Token Revocation (RFC 7009) | Full support |
|
|
168
|
-
| Token Exchange (RFC 8693) | Full support |
|
|
169
|
-
| OpenID Connect Core 1.0 | Core features |
|
|
170
|
-
| OIDC Discovery | Full support |
|
|
171
|
-
| OIDC RP-Initiated Logout | Full support |
|
|
172
|
-
|
|
173
|
-
## Related Packages
|
|
174
|
-
|
|
175
|
-
| Package | Description |
|
|
176
|
-
|---------|-------------|
|
|
177
|
-
| `@authrim/web` | Browser implementation (planned) |
|
|
178
|
-
| `@authrim/react` | React hooks and components (planned) |
|
|
179
|
-
| `@authrim/svelte` | Svelte/SvelteKit integration (planned) |
|
|
180
|
-
|
|
181
|
-
## Requirements
|
|
182
|
-
|
|
183
|
-
- Node.js >= 18
|
|
184
|
-
- TypeScript >= 5.0
|
|
185
|
-
|
|
186
|
-
## License
|
|
187
|
-
|
|
188
|
-
Apache-2.0
|
|
1
|
+
# @authrim/core
|
|
2
|
+
|
|
3
|
+
Platform-agnostic core library for [Authrim](https://github.com/authrim) - a modern, developer-friendly Identity Provider.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
This is the core package that provides OAuth 2.0 / OpenID Connect functionality without platform-specific dependencies. For most applications, use a framework-specific package (e.g., `@authrim/react`, `@authrim/web`) which includes this as a dependency.
|
|
8
|
+
|
|
9
|
+
**Use this package directly if you're:**
|
|
10
|
+
- Building a custom integration
|
|
11
|
+
- Creating a new platform-specific package
|
|
12
|
+
- Need full control over HTTP, crypto, and storage implementations
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @authrim/core
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Features
|
|
21
|
+
|
|
22
|
+
- **Authorization Code Flow with PKCE** - RFC 7636 compliant
|
|
23
|
+
- **Automatic Token Refresh** - With request coalescing for concurrent calls
|
|
24
|
+
- **Token Exchange** - RFC 8693 for delegation and cross-service scenarios
|
|
25
|
+
- **Token Introspection** - RFC 7662 server-side validation
|
|
26
|
+
- **Token Revocation** - RFC 7009 explicit invalidation
|
|
27
|
+
- **Silent Authentication** - prompt=none foundation
|
|
28
|
+
- **RP-Initiated Logout** - OIDC logout with optional token revocation
|
|
29
|
+
- **OIDC Discovery** - Automatic endpoint discovery with caching
|
|
30
|
+
- **Event System** - Subscribe to authentication lifecycle events
|
|
31
|
+
- **Error Recovery Metadata** - Guidance for retry logic and user actions
|
|
32
|
+
|
|
33
|
+
## Usage
|
|
34
|
+
|
|
35
|
+
When using `@authrim/core` directly, you must provide platform implementations:
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
import { createAuthrimClient } from '@authrim/core';
|
|
39
|
+
|
|
40
|
+
const client = await createAuthrimClient({
|
|
41
|
+
issuer: 'https://your-idp.com',
|
|
42
|
+
clientId: 'your-client-id',
|
|
43
|
+
crypto: yourCryptoProvider, // CryptoProvider interface
|
|
44
|
+
storage: yourStorageProvider, // AuthrimStorage interface
|
|
45
|
+
http: yourHttpProvider, // HttpClient interface
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
// Start login
|
|
49
|
+
const { url } = await client.buildAuthorizationUrl({
|
|
50
|
+
redirectUri: 'https://your-app.com/callback',
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// Handle callback
|
|
54
|
+
const tokens = await client.handleCallback(callbackUrl);
|
|
55
|
+
|
|
56
|
+
// Use tokens
|
|
57
|
+
const accessToken = await client.token.getAccessToken();
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## API Reference
|
|
61
|
+
|
|
62
|
+
### Token Operations
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
// Get access token (auto-refreshes if needed)
|
|
66
|
+
const accessToken = await client.token.getAccessToken();
|
|
67
|
+
|
|
68
|
+
// Get all tokens
|
|
69
|
+
const tokens = await client.token.getTokens();
|
|
70
|
+
|
|
71
|
+
// Token Exchange (RFC 8693)
|
|
72
|
+
const result = await client.token.exchange({
|
|
73
|
+
subjectToken: currentToken,
|
|
74
|
+
audience: 'https://api.other-service.com',
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
// Token Introspection (RFC 7662)
|
|
78
|
+
const info = await client.token.introspect({ token: accessToken });
|
|
79
|
+
|
|
80
|
+
// Token Revocation (RFC 7009)
|
|
81
|
+
await client.token.revoke({ token: accessToken });
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### Session & Logout
|
|
85
|
+
|
|
86
|
+
```typescript
|
|
87
|
+
// Check authentication status
|
|
88
|
+
const isAuth = await client.isAuthenticated();
|
|
89
|
+
|
|
90
|
+
// Get user info
|
|
91
|
+
const user = await client.getUser();
|
|
92
|
+
|
|
93
|
+
// Logout with token revocation
|
|
94
|
+
const { logoutUrl } = await client.logout({
|
|
95
|
+
revokeTokens: true,
|
|
96
|
+
postLogoutRedirectUri: 'https://your-app.com',
|
|
97
|
+
});
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Events
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
client.on('token:refreshed', ({ tokens }) => {
|
|
104
|
+
console.log('Token refreshed');
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
client.on('token:error', ({ error }) => {
|
|
108
|
+
if (error.meta.retryable) {
|
|
109
|
+
// Retry after error.meta.retryAfterMs
|
|
110
|
+
} else if (error.meta.userAction === 'reauthenticate') {
|
|
111
|
+
// Redirect to login
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
client.on('session:ended', ({ reason }) => {
|
|
116
|
+
console.log('Session ended:', reason);
|
|
117
|
+
});
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Error Handling
|
|
121
|
+
|
|
122
|
+
All errors include recovery metadata:
|
|
123
|
+
|
|
124
|
+
```typescript
|
|
125
|
+
try {
|
|
126
|
+
await client.token.getAccessToken();
|
|
127
|
+
} catch (error) {
|
|
128
|
+
if (error instanceof AuthrimError) {
|
|
129
|
+
console.log('Error code:', error.code);
|
|
130
|
+
console.log('Retryable:', error.meta.retryable);
|
|
131
|
+
console.log('User action:', error.meta.userAction);
|
|
132
|
+
// 'retry' | 'reauthenticate' | 'contact_support' | 'check_network' | 'none'
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## Provider Interfaces
|
|
138
|
+
|
|
139
|
+
Implement these interfaces for your platform:
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
interface CryptoProvider {
|
|
143
|
+
randomBytes(length: number): Uint8Array;
|
|
144
|
+
sha256(data: string | Uint8Array): Promise<Uint8Array>;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
interface AuthrimStorage {
|
|
148
|
+
get(key: string): Promise<string | null>;
|
|
149
|
+
set(key: string, value: string): Promise<void>;
|
|
150
|
+
remove(key: string): Promise<void>;
|
|
151
|
+
getAll(): Promise<Record<string, string>>;
|
|
152
|
+
clear(): Promise<void>;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
interface HttpClient {
|
|
156
|
+
fetch<T>(url: string, options?: HttpOptions): Promise<HttpResponse<T>>;
|
|
157
|
+
}
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## Standards Compliance
|
|
161
|
+
|
|
162
|
+
| Standard | Status |
|
|
163
|
+
|----------|--------|
|
|
164
|
+
| OAuth 2.0 (RFC 6749) | Authorization Code Flow |
|
|
165
|
+
| PKCE (RFC 7636) | S256 method |
|
|
166
|
+
| Token Introspection (RFC 7662) | Full support |
|
|
167
|
+
| Token Revocation (RFC 7009) | Full support |
|
|
168
|
+
| Token Exchange (RFC 8693) | Full support |
|
|
169
|
+
| OpenID Connect Core 1.0 | Core features |
|
|
170
|
+
| OIDC Discovery | Full support |
|
|
171
|
+
| OIDC RP-Initiated Logout | Full support |
|
|
172
|
+
|
|
173
|
+
## Related Packages
|
|
174
|
+
|
|
175
|
+
| Package | Description |
|
|
176
|
+
|---------|-------------|
|
|
177
|
+
| `@authrim/web` | Browser implementation (planned) |
|
|
178
|
+
| `@authrim/react` | React hooks and components (planned) |
|
|
179
|
+
| `@authrim/svelte` | Svelte/SvelteKit integration (planned) |
|
|
180
|
+
|
|
181
|
+
## Requirements
|
|
182
|
+
|
|
183
|
+
- Node.js >= 18
|
|
184
|
+
- TypeScript >= 5.0
|
|
185
|
+
|
|
186
|
+
## License
|
|
187
|
+
|
|
188
|
+
Apache-2.0
|