Modules@robinpath/oauth
oauth

@robinpath/oauth

0.1.2Node.jsPublic

OAuth 2.0 authorization flows and PKCE for RobinPath

OAuth

OAuth 2.0 authorization flows: auth URL, code exchange, refresh, client credentials, PKCE, token management

Package: @robinpath/oauth | Category: Web | Type: Utility

Authentication

No authentication required. All functions are available immediately.

Use Cases

Use the oauth module when you need to:

  • Build an OAuth 2.0 authorization URL with required parameters -- Use oauth.authUrl to perform this operation
  • Exchange an authorization code for access and refresh tokens -- Use oauth.exchangeCode to perform this operation
  • Refresh an expired access token using a refresh token -- Use oauth.refreshToken to perform this operation
  • Get an access token using the client credentials grant (machine-to-machine) -- Use oauth.clientCredentials to perform this operation
  • Generate a cryptographically random PKCE code verifier -- Use oauth.pkceVerifier to perform this operation

Quick Reference

FunctionDescriptionReturns
authUrlBuild an OAuth 2.0 authorization URL with required parametersComplete authorization URL
exchangeCodeExchange an authorization code for access and refresh tokens{accessToken, refreshToken, tokenType, expiresIn, scope}
refreshTokenRefresh an expired access token using a refresh token{accessToken, refreshToken, tokenType, expiresIn}
clientCredentialsGet an access token using the client credentials grant (machine-to-machine){accessToken, tokenType, expiresIn, scope}
pkceVerifierGenerate a cryptographically random PKCE code verifierURL-safe base64 encoded verifier string
pkceChallengeGenerate a PKCE code challenge from a verifier{challenge, method}
getTokenRetrieve a stored OAuth token by name{accessToken, refreshToken, tokenType, expired, expiresAt, scope} or null
isExpiredCheck if a stored token is expired (with buffer time)True if token is expired or will expire within buffer
generateStateGenerate a cryptographically random state parameter for CSRF protectionRandom hex string
revokeTokenRevoke an OAuth token at the provider's revocation endpoint{revoked: boolean, status: number}
clearTokensClear stored tokens by name or all tokensTrue if cleared

Functions

authUrl

Build an OAuth 2.0 authorization URL with required parameters

Module: oauth | Returns: string -- Complete authorization URL

oauth.authUrl "https://accounts.google.com/o/oauth2/v2/auth" {"clientId": "...", "scope": "email profile"}
ParameterTypeRequiredDescription
baseUrlstringYesAuthorization endpoint URL
optionsobjectYes{clientId, redirectUri, scope, state, responseType, codeChallenge, codeChallengeMethod, accessType, prompt}

exchangeCode

Exchange an authorization code for access and refresh tokens

Module: oauth | Returns: object -- {accessToken, refreshToken, tokenType, expiresIn, scope}

oauth.exchangeCode "https://oauth2.googleapis.com/token" {"code": "...", "clientId": "..."}
ParameterTypeRequiredDescription
tokenUrlstringYesToken endpoint URL
optionsobjectYes{code, clientId, clientSecret, redirectUri, codeVerifier, name}

refreshToken

Refresh an expired access token using a refresh token

Module: oauth | Returns: object -- {accessToken, refreshToken, tokenType, expiresIn}

oauth.refreshToken "https://oauth2.googleapis.com/token" {"name": "google", "clientId": "..."}
ParameterTypeRequiredDescription
tokenUrlstringYesToken endpoint URL
optionsobjectYes{refreshToken, clientId, clientSecret, scope, name}

clientCredentials

Get an access token using the client credentials grant (machine-to-machine)

Module: oauth | Returns: object -- {accessToken, tokenType, expiresIn, scope}

oauth.clientCredentials "https://api.example.com/oauth/token" {"clientId": "...", "clientSecret": "..."}
ParameterTypeRequiredDescription
tokenUrlstringYesToken endpoint URL
optionsobjectYes{clientId, clientSecret, scope, name}

pkceVerifier

Generate a cryptographically random PKCE code verifier

Module: oauth | Returns: string -- URL-safe base64 encoded verifier string

oauth.pkceVerifier 64
ParameterTypeRequiredDescription
lengthnumberNoVerifier length 43-128 (default 64)

pkceChallenge

Generate a PKCE code challenge from a verifier

Module: oauth | Returns: object -- {challenge, method}

oauth.pkceChallenge $verifier "S256"
ParameterTypeRequiredDescription
verifierstringYesThe code verifier
methodstringNo'S256' or 'plain' (default S256)

getToken

Retrieve a stored OAuth token by name

Module: oauth | Returns: object -- {accessToken, refreshToken, tokenType, expired, expiresAt, scope} or null

oauth.getToken "google"
ParameterTypeRequiredDescription
namestringYesToken store name

isExpired

Check if a stored token is expired (with buffer time)

Module: oauth | Returns: boolean -- True if token is expired or will expire within buffer

oauth.isExpired "google" 120000
ParameterTypeRequiredDescription
namestringYesToken store name
bufferMsnumberNoBuffer time in ms before expiry (default 60000)

generateState

Generate a cryptographically random state parameter for CSRF protection

Module: oauth | Returns: string -- Random hex string

oauth.generateState
ParameterTypeRequiredDescription
lengthnumberNoState length in bytes (default 32)

revokeToken

Revoke an OAuth token at the provider's revocation endpoint

Module: oauth | Returns: object -- {revoked: boolean, status: number}

oauth.revokeToken "https://oauth2.googleapis.com/revoke" {"name": "google"}
ParameterTypeRequiredDescription
revokeUrlstringYesRevocation endpoint URL
optionsobjectYes{token, name, tokenTypeHint, clientId, clientSecret}

clearTokens

Clear stored tokens by name or all tokens

Module: oauth | Returns: boolean -- True if cleared

oauth.clearTokens "google"
ParameterTypeRequiredDescription
namestringNoToken name to clear (omit to clear all)

Error Handling

All functions throw on failure. Common errors:

ErrorCause
Authorization URL is requiredCheck the error message for details
Token URL is requiredCheck the error message for details
Refresh token is requiredCheck the error message for details
OAuth refresh failed: ${data.error ?? response.statusText}Check the error message for details
OAuth client credentials failed: ${data.error ?? response.statusText}Check the error message for details
Revoke URL is requiredCheck the error message for details
Token is requiredCheck the error message for details
OAuth token exchange failed: ... - ...Check the error message for details
@desc "Auth url and validate result"
do
  set $result as oauth.authUrl "https://accounts.google.com/o/oauth2/v2/auth" {"clientId": "...", "scope": "email profile"}
  if $result != null
    print "Success"
  else
    print "No result"
  end
enddo

Recipes

1. List and iterate Token

Retrieve all items and loop through them.

@desc "Get token and iterate results"
do
  set $result as oauth.getToken "google"
  each $item in $result
    print $item
  end
enddo

2. Multi-step OAuth workflow

Chain multiple oauth operations together.

@desc "Auth url, exchange code, and more"
do
  set $r_authUrl as oauth.authUrl "https://accounts.google.com/o/oauth2/v2/auth" {"clientId": "...", "scope": "email profile"}
  set $r_exchangeCode as oauth.exchangeCode "https://oauth2.googleapis.com/token" {"code": "...", "clientId": "..."}
  set $r_refreshToken as oauth.refreshToken "https://oauth2.googleapis.com/token" {"name": "google", "clientId": "..."}
  print "All operations complete"
enddo

3. Safe authUrl with validation

Check results before proceeding.

@desc "Auth url and validate result"
do
  set $result as oauth.authUrl "https://accounts.google.com/o/oauth2/v2/auth" {"clientId": "...", "scope": "email profile"}
  if $result != null
    print "Success: " + $result
  else
    print "Operation returned no data"
  end
enddo

Related Modules

  • json -- JSON module for complementary functionality

Versions (1)

VersionTagPublished
0.1.2latest1 months ago
Install
$ robinpath add @robinpath/oauth

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.2
LicenseMIT
Unpacked Size5.9 KB
Versions1
Weekly Downloads21
Total Downloads21
Stars0
Last Publish1 months ago
Created1 months ago

Keywords

Category

web