-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f292061
commit 3eb7ef3
Showing
49 changed files
with
282 additions
and
1,448 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1 @@ | ||
# @oslojs/oauth2 | ||
|
||
## 0.3.0 | ||
|
||
- [Breaking] Remove `setScopes()` and rename `appendScopes()` to `addScopes()` | ||
|
||
## 0.2.0 | ||
|
||
- [Breaking] Remove APIs for sending requests | ||
- Add `RequestResult`s | ||
|
||
## 0.1.2 | ||
|
||
- Update dependencies. | ||
|
||
## 0.1.1 | ||
|
||
- Fix: Add `Basic` prefix to encoded credentials in `authenticateWithHTTPBasicAuth()` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
--- | ||
title: "Parsing responses" | ||
--- | ||
|
||
# Parsing responses | ||
|
||
## Access token and refresh token requests | ||
|
||
Use `TokenRequestResult` for parsing access token and refresh token responses from the token endpoint. Methods like `accessToken()` will either return a value or throw an error if the field doesn't exist. | ||
|
||
```ts | ||
import { TokenRequestResult } from "@oslojs/oauth2"; | ||
|
||
const response = await fetch(accessTokenRequest); | ||
const data = await response.json(); | ||
if (typeof data !== "object" || data === null) { | ||
throw new Error("Unexpected response body"); | ||
} | ||
const result = new TokenRequestResult(data); | ||
if (result.hasErrorCode()) { | ||
const error = result.errorCode(); | ||
throw new Error(`Failed to revoke token: ${error}`); | ||
} | ||
try { | ||
const accessToken = result.accessToken(); | ||
const accessTokenExpiresAt = result.accessTokenExpiresAt(); | ||
const refreshToken = result.refreshToken(); | ||
} catch { | ||
throw new Error("Failed to parse response"); | ||
} | ||
``` | ||
|
||
## Token revocation requests | ||
|
||
Since the token revocation endpoint returns an empty response when successful, use `OAuthRequestResult` directly. | ||
|
||
```ts | ||
import { OAuthRequestResult } from "@oslojs/oauth2"; | ||
|
||
const response = await fetch(tokenRevocationRequest); | ||
if (!response.ok) { | ||
const data = await response.json(); | ||
if (typeof data !== "object" || data === null) { | ||
throw new Error("Unexpected response body"); | ||
} | ||
const result = new OAuthRequestResult(data); | ||
if (!result.hasErrorCode()) { | ||
throw new Error("Unexpected response body"); | ||
} | ||
const error = result.errorCode(); | ||
throw new Error(`Failed to revoke token: ${error}`); | ||
} | ||
``` | ||
|
||
## Device authorization requests | ||
|
||
Use `DeviceAuthorizationRequestResult` for parsing device authorization responses. Methods like `deviceCode()` will either return a value or throw an error if the field doesn't exist. | ||
|
||
```ts | ||
import { DeviceAuthorizationRequestResult } from "@oslojs/oauth2"; | ||
|
||
const response = await fetch(deviceAuthorizationRequest); | ||
const data = await response.json(); | ||
if (typeof data !== "object" || data === null) { | ||
throw new Error("Unexpected response body"); | ||
} | ||
const result = new DeviceAuthorizationRequestResult(data); | ||
if (result.hasErrorCode()) { | ||
const error = result.errorCode(); | ||
throw new Error(`Failed to revoke token: ${error}`); | ||
} | ||
try { | ||
const deviceCode = result.deviceCode(); | ||
const userCode = result.userCode(); | ||
const codesExpireIn = result.codesExpireIn(); | ||
} catch { | ||
throw new Error("Failed to parse response"); | ||
} | ||
``` |
Oops, something went wrong.