0
# Storage and State Management
1
2
Flexible storage options for user data and request state with built-in implementations and custom storage interfaces.
3
4
## Capabilities
5
6
### StateStore Interface
7
8
Core interface for state persistence.
9
10
```typescript { .api }
11
/**
12
* Interface for storing and retrieving state data
13
*/
14
interface StateStore {
15
set(key: string, value: string): Promise<void>;
16
get(key: string): Promise<string | null>;
17
remove(key: string): Promise<string | null>;
18
getAllKeys(): Promise<string[]>;
19
}
20
```
21
22
### WebStorageStateStore
23
24
Web storage implementation using localStorage or sessionStorage.
25
26
```typescript { .api }
27
/**
28
* StateStore implementation using Web Storage API
29
*/
30
class WebStorageStateStore implements StateStore {
31
constructor(args?: { store?: Storage; prefix?: string });
32
33
set(key: string, value: string): Promise<void>;
34
get(key: string): Promise<string | null>;
35
remove(key: string): Promise<string | null>;
36
getAllKeys(): Promise<string[]>;
37
}
38
```
39
40
### InMemoryWebStorage
41
42
In-memory storage implementation.
43
44
```typescript { .api }
45
/**
46
* In-memory storage implementation
47
*/
48
class InMemoryWebStorage implements StateStore {
49
setItem(key: string, value: string): void;
50
getItem(key: string): string | null;
51
removeItem(key: string): void;
52
key(index: number): string | null;
53
readonly length: number;
54
}
55
```
56
57
### DPoP Storage
58
59
DPoP (Demonstration of Proof-of-Possession) state management.
60
61
```typescript { .api }
62
interface DPoPStore {
63
set(key: string, value: string): Promise<void>;
64
get(key: string): Promise<string | null>;
65
remove(key: string): Promise<void>;
66
}
67
68
class IndexedDbDPoPStore implements DPoPStore {
69
constructor(dbName?: string, storeName?: string);
70
set(key: string, value: string): Promise<void>;
71
get(key: string): Promise<string | null>;
72
remove(key: string): Promise<void>;
73
}
74
75
class DPoPState {
76
constructor(args: { dpopJkt: string; nonce?: string });
77
readonly dpopJkt: string;
78
readonly nonce?: string;
79
}
80
```
81
82
## Usage Examples
83
84
```typescript
85
import { UserManager, WebStorageStateStore, InMemoryWebStorage } from "oidc-client-ts";
86
87
// Custom storage configuration
88
const userManager = new UserManager({
89
authority: "https://demo.identityserver.io",
90
client_id: "interactive.public",
91
redirect_uri: "http://localhost:3000/callback",
92
93
// User storage in localStorage
94
userStore: new WebStorageStateStore({
95
store: window.localStorage,
96
prefix: "myapp.user."
97
}),
98
99
// State storage in sessionStorage
100
stateStore: new WebStorageStateStore({
101
store: window.sessionStorage,
102
prefix: "myapp.state."
103
}),
104
});
105
106
// Custom storage implementation
107
class CustomStateStore implements StateStore {
108
private data = new Map<string, string>();
109
110
async set(key: string, value: string): Promise<void> {
111
this.data.set(key, value);
112
}
113
114
async get(key: string): Promise<string | null> {
115
return this.data.get(key) || null;
116
}
117
118
async remove(key: string): Promise<string | null> {
119
const value = this.data.get(key) || null;
120
this.data.delete(key);
121
return value;
122
}
123
124
async getAllKeys(): Promise<string[]> {
125
return Array.from(this.data.keys());
126
}
127
}
128
```
129
130
## DPoP (Demonstrating Proof-of-Possession) Support
131
132
### DPoPStore Interface
133
134
Storage interface for DPoP key pairs and nonce values used in OAuth 2.0 DPoP token binding.
135
136
```typescript { .api }
137
/**
138
* Interface for storing and retrieving DPoP state data
139
*/
140
interface DPoPStore {
141
set(key: string, value: DPoPState): Promise<void>;
142
get(key: string): Promise<DPoPState>;
143
remove(key: string): Promise<DPoPState>;
144
getAllKeys(): Promise<string[]>;
145
}
146
147
/**
148
* DPoP state containing key pairs and nonce
149
*/
150
class DPoPState {
151
constructor(
152
keys: CryptoKeyPair,
153
nonce?: string
154
);
155
156
readonly keys: CryptoKeyPair;
157
nonce?: string;
158
}
159
```
160
161
### IndexedDbDPoPStore
162
163
IndexedDB-based implementation for DPoP storage.
164
165
```typescript { .api }
166
/**
167
* DPoPStore implementation using IndexedDB for persistent storage
168
*/
169
class IndexedDbDPoPStore implements DPoPStore {
170
constructor(args?: {
171
dbName?: string;
172
tableName?: string;
173
});
174
175
set(key: string, value: DPoPState): Promise<void>;
176
get(key: string): Promise<DPoPState>;
177
remove(key: string): Promise<DPoPState>;
178
getAllKeys(): Promise<string[]>;
179
}
180
```
181
182
**Usage Example:**
183
184
```typescript
185
import { UserManager, IndexedDbDPoPStore, DPoPState } from "oidc-client-ts";
186
187
// Configure UserManager with DPoP support
188
const userManager = new UserManager({
189
authority: "https://your-oidc-provider.com",
190
client_id: "your-client-id",
191
redirect_uri: "http://localhost:3000/callback",
192
response_type: "code",
193
scope: "openid profile email",
194
dpop: {
195
bind_authorization_code: true,
196
store: new IndexedDbDPoPStore({
197
dbName: "myapp-dpop",
198
tableName: "dpop_keys"
199
})
200
}
201
});
202
```