@atproto/oauth-client-node 0.0.6 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +22 -13
- package/package.json +4 -5
package/README.md
CHANGED
@@ -36,7 +36,6 @@ const client = new NodeOAuthClientOptions({
|
|
36
36
|
tos_uri: 'https://my-app.com/tos',
|
37
37
|
policy_uri: 'https://my-app.com/policy',
|
38
38
|
redirect_uris: ['https://my-app.com/callback'],
|
39
|
-
scope: 'profile email offline_access',
|
40
39
|
grant_types: ['authorization_code', 'refresh_token'],
|
41
40
|
response_types: ['code'],
|
42
41
|
application_type: 'web',
|
@@ -105,12 +104,14 @@ app.get('/atproto-oauth-callback', async (req, res, next) => {
|
|
105
104
|
try {
|
106
105
|
const params = new URLSearchParams(req.url.split('?')[1])
|
107
106
|
|
108
|
-
const {
|
107
|
+
const { session, state } = await client.callback(params)
|
109
108
|
|
110
109
|
// Process successful authentication here
|
111
110
|
console.log('authorize() was called with state:', state)
|
112
111
|
|
113
|
-
console.log('User authenticated as:',
|
112
|
+
console.log('User authenticated as:', session.did)
|
113
|
+
|
114
|
+
const agent = new Agent(session)
|
114
115
|
|
115
116
|
// Make Authenticated API calls
|
116
117
|
const profile = await agent.getProfile({ actor: agent.did })
|
@@ -126,12 +127,14 @@ app.get('/atproto-oauth-callback', async (req, res, next) => {
|
|
126
127
|
async function worker() {
|
127
128
|
const userDid = 'did:plc:123'
|
128
129
|
|
129
|
-
const
|
130
|
+
const oauthSession = await client.restore(userDid)
|
130
131
|
|
131
|
-
// Note: If the current access_token is expired, the
|
132
|
+
// Note: If the current access_token is expired, the session will automatically
|
132
133
|
// (and transparently) refresh it. The new token set will be saved though
|
133
134
|
// the client's session store.
|
134
135
|
|
136
|
+
const agent = new Agent(oauthSession)
|
137
|
+
|
135
138
|
// Make Authenticated API calls
|
136
139
|
const profile = await agent.getProfile({ actor: agent.did })
|
137
140
|
console.log('Bsky profile:', profile.data)
|
@@ -154,7 +157,7 @@ The client metadata will typically contain:
|
|
154
157
|
"tos_uri": "https://my-app.com/tos",
|
155
158
|
"policy_uri": "https://my-app.com/policy",
|
156
159
|
"redirect_uris": ["https://my-app.com/atproto-oauth-callback"],
|
157
|
-
"scope": "
|
160
|
+
"scope": "atproto",
|
158
161
|
"grant_types": ["authorization_code", "refresh_token"],
|
159
162
|
"response_types": ["code"],
|
160
163
|
"application_type": "native",
|
@@ -290,7 +293,8 @@ list of examples below). Any refresh of the credentials will happen under the
|
|
290
293
|
hood, and the new tokens will be saved in the session store.
|
291
294
|
|
292
295
|
```ts
|
293
|
-
const
|
296
|
+
const session = await client.restore('did:plc:123')
|
297
|
+
const agent = new Agent(session)
|
294
298
|
|
295
299
|
// Feeds and content
|
296
300
|
await agent.getTimeline(params, opts)
|
@@ -339,9 +343,8 @@ await agent.updateHandle(params, opts)
|
|
339
343
|
|
340
344
|
// etc.
|
341
345
|
|
342
|
-
|
343
|
-
|
344
|
-
}
|
346
|
+
// Always remember to revoke the credentials when you are done
|
347
|
+
await session.signOut()
|
345
348
|
```
|
346
349
|
|
347
350
|
## Advances use-cases
|
@@ -379,7 +382,7 @@ client.addEventListener(
|
|
379
382
|
// - session data does not match expected values returned by the OAuth server
|
380
383
|
} else if (cause instanceof TokenRevokedError) {
|
381
384
|
// Session was revoked through:
|
382
|
-
// -
|
385
|
+
// - session.signOut()
|
383
386
|
// - client.revoke(sub)
|
384
387
|
} else {
|
385
388
|
// An unexpected error occurred, causing the session to be deleted
|
@@ -415,9 +418,15 @@ app.get('/atproto-oauth-callback', async (req, res) => {
|
|
415
418
|
const params = new URLSearchParams(req.url.split('?')[1])
|
416
419
|
try {
|
417
420
|
try {
|
418
|
-
const {
|
421
|
+
const { session, state } = await client.callback(params)
|
422
|
+
|
423
|
+
// Process successful authentication here. For example:
|
424
|
+
|
425
|
+
const agent = new Agent(session)
|
426
|
+
|
427
|
+
const profile = await agent.getProfile({ actor: agent.did })
|
419
428
|
|
420
|
-
|
429
|
+
console.log('Bsky profile:', profile.data)
|
421
430
|
} catch (err) {
|
422
431
|
// Silent sign-in failed, retry without prompt=none
|
423
432
|
if (
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@atproto/oauth-client-node",
|
3
|
-
"version": "0.0
|
3
|
+
"version": "0.1.0",
|
4
4
|
"license": "MIT",
|
5
5
|
"description": "ATPROTO OAuth client for the NodeJS",
|
6
6
|
"keywords": [
|
@@ -35,12 +35,11 @@
|
|
35
35
|
"@atproto/jwk": "0.1.1",
|
36
36
|
"@atproto/jwk-jose": "0.1.2",
|
37
37
|
"@atproto/jwk-webcrypto": "0.1.2",
|
38
|
-
"@atproto/oauth-client": "0.
|
39
|
-
"@atproto/oauth-types": "0.1.
|
38
|
+
"@atproto/oauth-client": "0.2.0",
|
39
|
+
"@atproto/oauth-types": "0.1.4"
|
40
40
|
},
|
41
41
|
"devDependencies": {
|
42
|
-
"typescript": "^5.3.3"
|
43
|
-
"@atproto/api": "0.13.2"
|
42
|
+
"typescript": "^5.3.3"
|
44
43
|
},
|
45
44
|
"scripts": {
|
46
45
|
"build": "tsc --build tsconfig.build.json"
|