### Installation Source: https://github.com/oslo-project/oauth2/blob/main/docs/pages/index.md Provides the npm command to install the @oslojs/oauth2 package. ```bash npm i @oslojs/oauth2 ``` -------------------------------- ### Install Dependencies Source: https://github.com/oslo-project/oauth2/blob/main/CONTRIBUTING.md Installs project dependencies using PNPM. This is the first step to set up the development environment. ```shell pnpm i ``` -------------------------------- ### Install @oslojs/oauth2 Source: https://github.com/oslo-project/oauth2/blob/main/README.md Provides the npm command to install the @oslojs/oauth2 package. ```bash npm i @oslojs/oauth2 ``` -------------------------------- ### Changeset Summary Examples Source: https://github.com/oslo-project/oauth2/blob/main/CONTRIBUTING.md Examples of summaries to write in the changeset markdown file, indicating the type of change (Fix or Feat) and a brief description. ```markdown Fix: Handle negative numbers in `sqrt()` ``` ```markdown Feat: Add `greet()` ``` -------------------------------- ### OAuth2 Module Reference Source: https://github.com/oslo-project/oauth2/blob/main/docs/pages/reference/index.md This section provides a comprehensive reference for the @oslojs/oauth2 module, outlining its core functionalities and how to integrate them into your projects. It details the available methods, their parameters, return values, and usage examples. ```APIDOC Module: @oslojs/oauth2 This module provides utilities for working with OAuth2 authorization flows and token management. Refer to the following for detailed API documentation: - Main API: /reference/main ``` -------------------------------- ### Get Verification URI Source: https://github.com/oslo-project/oauth2/blob/main/docs/pages/reference/main/DeviceAuthorizationRequestResult/verificationURI.md Retrieves the verification URI from the DeviceAuthorizationRequestResult. It ensures the field exists and is a string, otherwise it throws an error. ```ts function verificationURI(): string; ``` -------------------------------- ### Get Device Code Source: https://github.com/oslo-project/oauth2/blob/main/docs/pages/reference/main/DeviceAuthorizationRequestResult/deviceCode.md Retrieves the 'device_code' from the DeviceAuthorizationRequestResult. This method expects the 'device_code' field to be present and a string. It will throw an error if these conditions are not met. ```ts function deviceCode(): string; ``` -------------------------------- ### Get Access Token Source: https://github.com/oslo-project/oauth2/blob/main/docs/pages/reference/main/TokenRequestResult/accessToken.md Retrieves the access token from the result. This function expects the 'access_token' field to be present and a string. It will throw an error if these conditions are not met. ```typescript function accessToken(): string; ``` -------------------------------- ### Get User Code from DeviceAuthorizationRequestResult Source: https://github.com/oslo-project/oauth2/blob/main/docs/pages/reference/main/DeviceAuthorizationRequestResult/userCode.md This TypeScript function retrieves the 'user_code' from a DeviceAuthorizationRequestResult object. It includes error handling to ensure the 'user_code' field exists and is a string. This is crucial for OAuth 2.0 device authorization flows where the user code is presented to the end-user. ```ts function userCode(): string; ``` -------------------------------- ### Get Expiration Date Source: https://github.com/oslo-project/oauth2/blob/main/docs/pages/reference/main/DeviceAuthorizationRequestResult/codesExpireAt.md This function retrieves the `expires_in` field from a DeviceAuthorizationRequestResult and converts it into a `Date` object representing the expiration time. It includes error handling for missing fields or non-numeric values. ```ts function codesExpireAt(): Date; ``` -------------------------------- ### Get Access Token Expiration Date Source: https://github.com/oslo-project/oauth2/blob/main/docs/pages/reference/main/TokenRequestResult/accessTokenExpiresAt.md Retrieves the expiration date of the access token. This function expects an `expires_in` field to be present and to be a number. It returns the expiration as a `Date` object. An error is thrown if the field is missing or not a number. ```typescript function accessTokenExpiresAt(): Date; ``` -------------------------------- ### Get Token Type Source: https://github.com/oslo-project/oauth2/blob/main/docs/pages/reference/main/TokenRequestResult/tokenType.md Retrieves the 'token_type' from the result of an OAuth2 token request. This method expects the 'token_type' field to be present and a string; otherwise, it will throw an error. ```ts function tokenType(): string; ``` -------------------------------- ### Build Package Source: https://github.com/oslo-project/oauth2/blob/main/CONTRIBUTING.md Builds the project package. This command compiles the source code and prepares it for testing or deployment. ```shell pnpm build ``` -------------------------------- ### Create Changesets Source: https://github.com/oslo-project/oauth2/blob/main/CONTRIBUTING.md Creates a changeset for tracking changes in the project. Use 'minor' for new features and 'patch' for bug fixes. A new markdown file is created in the `.changesets` directory to summarize the changes. ```shell pnpm auri add minor ``` ```shell pnpm auri add patch ``` -------------------------------- ### TokenRequestResult API Documentation Source: https://github.com/oslo-project/oauth2/blob/main/docs/pages/reference/main/TokenRequestResult/index.md Provides a comprehensive reference for the methods available in TokenRequestResult, including token details, refresh token handling, scopes, and token type. Also lists inherited methods from OAuth2RequestResult. ```APIDOC TokenRequestResult: Extends: OAuth2RequestResult Constructor: constructor(body: object): this body: JSON.parsed()-ed object. Methods: # Inherited from OAuth2RequestResult OAuth2RequestResult.errorCode(): any OAuth2RequestResult.errorDescription(): string | null OAuth2RequestResult.errorURI(): string | null OAuth2RequestResult.hasErrorCode(): boolean OAuth2RequestResult.hasErrorDescription(): boolean OAuth2RequestResult.hasErrorURI(): boolean OAuth2RequestResult.state(): string | null # TokenRequestResult specific methods TokenRequestResult.accessToken(): string | null TokenRequestResult.accessTokenExpiresAt(): Date | null TokenRequestResult.accessTokenExpiresInSeconds(): number | null TokenRequestResult.hasRefreshToken(): boolean TokenRequestResult.hasScopes(): boolean TokenRequestResult.refreshToken(): string | null TokenRequestResult.scopes(): string[] | null TokenRequestResult.tokenType(): string | null Properties: body: object ``` -------------------------------- ### OAuth2RequestResult Constructor and Methods Source: https://github.com/oslo-project/oauth2/blob/main/docs/pages/reference/main/OAuth2RequestResult/index.md Defines the constructor for OAuth2RequestResult and lists its associated methods for accessing error details and state information. The constructor takes a JSON-parsed object as input. ```APIDOC OAuth2RequestResult: constructor(body: object): this body: JSON-parsed object. Methods: errorCode(): Returns the error code if present. errorDescription(): Returns the error description if present. errorURI(): Returns the error URI if present. hasErrorCode(): Checks if an error code is present. hasErrorDescription(): Checks if an error description is present. hasErrorURI(): Checks if an error URI is present. hasState(): Checks if a state parameter is present. state(): Returns the state parameter if present. ``` -------------------------------- ### OAuth2 Classes Source: https://github.com/oslo-project/oauth2/blob/main/docs/pages/reference/main/index.md This section outlines the core classes provided by the @oslojs/oauth2 library for managing OAuth2 flows. These classes encapsulate the results of device authorization, general OAuth2 requests, and token requests. ```javascript class DeviceAuthorizationRequestResult { // ... properties and methods for device authorization results } class OAuth2RequestResult { // ... properties and methods for general OAuth2 request results } class TokenRequestResult { // ... properties and methods for token request results } ``` -------------------------------- ### DeviceAuthorizationRequestResult Constructor and Properties Source: https://github.com/oslo-project/oauth2/blob/main/docs/pages/reference/main/DeviceAuthorizationRequestResult/index.md Defines the constructor for DeviceAuthorizationRequestResult which takes a JSON-parsed body and outlines the structure of its properties. ```ts function constructor(body: object): this; interface Properties { body: object; } ``` -------------------------------- ### DeviceAuthorizationRequestResult API Methods Source: https://github.com/oslo-project/oauth2/blob/main/docs/pages/reference/main/DeviceAuthorizationRequestResult/index.md Lists the methods available for DeviceAuthorizationRequestResult, including inherited methods from OAuth2RequestResult and specific methods for device authorization. ```APIDOC DeviceAuthorizationRequestResult: constructor(body: object): this body: JSON-parsed object. codesExpireAt(): Date | undefined Returns the expiration date of the codes. codesExpireIn(): number | undefined Returns the time in seconds until the codes expire. deviceCode(): string | undefined Returns the device verification code. intervalSeconds(): number | undefined Returns the minimum time in seconds the client should wait between polling requests. userCode(): string | undefined Returns the end-user verification code. verificationURI(): string | undefined Returns the end-user verification URI. OAuth2RequestResult (Inherited Methods): errorCode(): string | undefined Returns the error code, if any. errorDescription(): string | undefined Returns the error description, if any. errorURI(): string | undefined Returns the URI for further information about the error, if any. hasErrorCode(): boolean Checks if an error code is present. hasErrorDescription(): boolean Checks if an error description is present. hasErrorURI(): boolean Checks if an error URI is present. state(): string | undefined Returns the state parameter, if any. ``` -------------------------------- ### TokenRequestResult Constructor and Properties Source: https://github.com/oslo-project/oauth2/blob/main/docs/pages/reference/main/TokenRequestResult/index.md Defines the constructor for TokenRequestResult which accepts a parsed JSON body. Also outlines the properties available, including the inherited 'body' property. ```ts function constructor(body: object): this; interface Properties { body: object; } ``` -------------------------------- ### Parse OAuth 2.0 Token Response Source: https://github.com/oslo-project/oauth2/blob/main/README.md Demonstrates how to parse a successful OAuth 2.0 token response using the TokenRequestResult class. It handles potential errors and extracts key information like access token, expiration, and refresh token. ```typescript import { TokenRequestResult } from "@oslojs/oauth2"; const response = await fetch("https://github.com/login/oauth/access_token", { method: "POST", body, headers }); const data = await response.json(); if (typeof data !== "object" || data === null) { throw new Error("Unexpected response"); } const result = new TokenRequestResult(data); if (result.hasErrorCode()) { const error = result.errorCode(); throw new Error(`Request failed: ${error}`); } const accessToken = result.accessToken(); const accessTokenExpiresAt = result.accessTokenExpiresAt(); const refreshToken = result.refreshToken(); ``` -------------------------------- ### Token Request Result Parsing Source: https://github.com/oslo-project/oauth2/blob/main/docs/pages/index.md Demonstrates how to parse a token request response using the TokenRequestResult class from @oslojs/oauth2. It handles potential errors and extracts access tokens, expiration times, and refresh tokens. ```typescript import { TokenRequestResult } from "@oslojs/oauth2"; const response = await fetch("https://github.com/login/oauth/access_token", { method: "POST", body, headers }); const data = await response.json(); if (typeof data !== "object" || data === null) { throw new Error("Unexpected response"); } const result = new TokenRequestResult(data); if (result.hasErrorCode()) { const error = result.errorCode(); throw new Error(`Request failed: ${error}`); } const accessToken = result.accessToken(); const accessTokenExpiresAt = result.accessTokenExpiresAt(); const refreshToken = result.refreshToken(); ``` -------------------------------- ### OAuth2RequestResult.errorDescription() Method Source: https://github.com/oslo-project/oauth2/blob/main/docs/pages/reference/main/OAuth2RequestResult/errorDescription.md Returns the `error_description` field value from an OAuth2 request result. This method will throw an `Error` if the `error_description` field is missing or if its value is not a string. It is a core method for accessing detailed error information in OAuth2 flows. ```ts function errorDescription(): string; ``` -------------------------------- ### OAuth2RequestResult.state() Definition Source: https://github.com/oslo-project/oauth2/blob/main/docs/pages/reference/main/OAuth2RequestResult/state.md Defines the signature for the state() function, which returns a string representing the state parameter. It is part of the OAuth2RequestResult interface. ```ts function state(): string; ``` -------------------------------- ### Parse Access Token and Refresh Token Responses Source: https://github.com/oslo-project/oauth2/blob/main/docs/pages/examples/parse.md Parses responses from OAuth2 token endpoints using `TokenRequestResult`. It handles extracting access tokens, their expiration times, and refresh tokens. Errors are thrown if the response is malformed or if required fields are missing. ```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"); } ``` -------------------------------- ### Parse Token Revocation Responses Source: https://github.com/oslo-project/oauth2/blob/main/docs/pages/examples/parse.md Handles parsing responses from token revocation endpoints using `OAuthRequestResult`. Since successful revocation returns an empty response, this snippet checks for HTTP success and parses potential error details if the request fails. ```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}`); } ``` -------------------------------- ### Parse Device Authorization Responses Source: https://github.com/oslo-project/oauth2/blob/main/docs/pages/examples/parse.md Parses responses from OAuth2 device authorization endpoints using `DeviceAuthorizationRequestResult`. It extracts device codes, user codes, and expiration times for codes. Errors are handled for malformed responses or missing fields. ```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"); } ``` -------------------------------- ### OAuth2RequestResult Properties Source: https://github.com/oslo-project/oauth2/blob/main/docs/pages/reference/main/OAuth2RequestResult/index.md Defines the properties available for the OAuth2RequestResult class, specifically the 'body' property which holds the JSON-parsed response. ```ts interface Properties { body: object; } ``` -------------------------------- ### TokenRequestResult.scopes() Definition Source: https://github.com/oslo-project/oauth2/blob/main/docs/pages/reference/main/TokenRequestResult/scopes.md Defines the `scopes` function which retrieves and parses the 'scope' field. It returns a number, but the description implies it parses into an array. Throws an error if the scope field is missing or not a string. ```ts function scopes(): number; ``` -------------------------------- ### TokenType Enum Definition Source: https://github.com/oslo-project/oauth2/blob/main/docs/pages/reference/main/TokenType.md Defines the possible types for tokens within the OAuth2 system. This enum includes AccessToken for granting access to resources and RefreshToken for obtaining new access tokens. ```ts enum TokenType { AccessToken, RefreshToken } ``` -------------------------------- ### DeviceAuthorizationRequestResult intervalSeconds() Source: https://github.com/oslo-project/oauth2/blob/main/docs/pages/reference/main/DeviceAuthorizationRequestResult/intervalSeconds.md Retrieves the interval field value from DeviceAuthorizationRequestResult. If the field is not defined, it returns the RFC-specified default of 5 seconds. The function will throw an Error if the interval value is not a number. ```ts function intervalSeconds(): number; ``` -------------------------------- ### OAuth2RequestResult.errorURI() Source: https://github.com/oslo-project/oauth2/blob/main/docs/pages/reference/main/OAuth2RequestResult/errorURI.md Returns the `error_uri` field value. Throws an `Error` if the field is missing or the value isn't a string. ```ts function errorURI(): string; ``` -------------------------------- ### DeviceAuthorizationRequestResult codesExpireInSeconds() Source: https://github.com/oslo-project/oauth2/blob/main/docs/pages/reference/main/DeviceAuthorizationRequestResult/codesExpireInSeconds.md This function retrieves the `expires_in` field from a DeviceAuthorizationRequestResult object. It is designed to return the expiration time in seconds. An error will be thrown if the `expires_in` field is not present or if its value is not a valid number. ```ts function codesExpireInSeconds(): number; ``` -------------------------------- ### Check for Error Description in OAuth2 Request Source: https://github.com/oslo-project/oauth2/blob/main/docs/pages/reference/main/OAuth2RequestResult/hasErrorDescription.md This function checks if the 'error_description' field is present in the OAuth2 request result and if its value is a string. It's useful for validating error responses. ```ts function hasErrorDescription(): boolean; ``` -------------------------------- ### OAuth2RequestResult.errorCode() Definition Source: https://github.com/oslo-project/oauth2/blob/main/docs/pages/reference/main/OAuth2RequestResult/errorCode.md Defines the `errorCode` function which returns the error field value from an OAuth2 request result. It is expected to return a string and will throw an error if the field is absent or not a string. ```ts function errorCode(): string; ``` -------------------------------- ### Check OAuth2 State Source: https://github.com/oslo-project/oauth2/blob/main/docs/pages/reference/main/OAuth2RequestResult/hasState.md Determines if the 'state' field in an OAuth2 request result is present and is a string. This is useful for validating the integrity of the OAuth2 flow. ```ts function hasState(): boolean; ``` -------------------------------- ### TokenRequestResult.refreshToken() Definition Source: https://github.com/oslo-project/oauth2/blob/main/docs/pages/reference/main/TokenRequestResult/refreshToken.md This TypeScript function returns the refresh_token field value from a TokenRequestResult object. It throws an error if the field is missing or not a string. ```ts function refreshToken(): string; ``` -------------------------------- ### Check Scope Existence and Type Source: https://github.com/oslo-project/oauth2/blob/main/docs/pages/reference/main/TokenRequestResult/hasScopes.md This function determines if the 'scope' field is present in the TokenRequestResult and if its value is of type string. It's a utility for validating scope data. ```typescript function hasScopes(): boolean; ``` -------------------------------- ### Check for Error URI in OAuth2RequestResult Source: https://github.com/oslo-project/oauth2/blob/main/docs/pages/reference/main/OAuth2RequestResult/hasErrorURI.md This function determines if the 'error_uri' field is present within the OAuth2RequestResult and if its value is a string. It's a boolean check for the existence and type of this specific field. ```ts function hasErrorURI(): boolean; ``` -------------------------------- ### OAuth2RequestResult.hasErrorCode() Source: https://github.com/oslo-project/oauth2/blob/main/docs/pages/reference/main/OAuth2RequestResult/hasErrorCode.md Returns true if the 'error' field exists and its value is a string. This method is useful for determining if an OAuth2 request returned an error. ```ts function hasErrorCode(): boolean; ``` -------------------------------- ### TokenRequestResult.accessTokenExpiresInSeconds() Definition Source: https://github.com/oslo-project/oauth2/blob/main/docs/pages/reference/main/TokenRequestResult/accessTokenExpiresInSeconds.md This TypeScript function retrieves the 'expires_in' field from a token request result. It expects the field to be a number and will throw an error if it's missing or not a number. ```ts function accessTokenExpiresInSeconds(): number; ``` -------------------------------- ### Check for Refresh Token in OAuth2 Request Result Source: https://github.com/oslo-project/oauth2/blob/main/docs/pages/reference/main/TokenRequestResult/hasRefreshToken.md This function determines if a refresh token is present in the OAuth2 request result by checking if the 'error' field exists and is a string. It's a boolean check for a specific condition within the result object. ```ts function hasRefreshToken(): boolean; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.