Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Publish

on:
push:
branches:
- main

jobs:
publish:
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write # The OIDC ID token is used for authentication with JSR.
steps:
- uses: actions/checkout@v4
- run: npx jsr publish
3 changes: 3 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ jobs:
with:
deno-version: ${{ matrix.deno }} # tests across multiple Deno versions

- name: Publish (dry run)
run: deno publish --dry-run

- name: Check types
run: deno task check:types

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ Currently supported OAuth 2.0 grants:
### GitHub API example using [oak](https://deno.land/x/oak)

```ts ignore
import { Application, Router } from "https://deno.land/x/[email protected]/mod.ts";
import { Session } from "https://deno.land/x/oak_sessions@v4.0.5/mod.ts";
import { OAuth2Client } from "https://deno.land/x/oauth2_client/mod.ts";
import { Application, Router } from "jsr:@oak/oak";
import { Session } from "https://deno.land/x/oak_sessions@v4.1.12/mod.ts";
import { OAuth2Client } from "jsr:@cmd-johnson/deno-oauth2-client";

const oauth2Client = new OAuth2Client({
clientId: Deno.env.get("CLIENT_ID")!,
Expand Down
10 changes: 9 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
{
"name": "@cmd-johnson/oauth2-client",
"version": "2.0.0",
"imports": {
"@oak/oak": "jsr:@oak/oak@^17.1.3",
"@std/assert": "jsr:@std/assert@^1.0.7",
"@std/encoding": "jsr:@std/encoding@^1.0.5",
"@std/http": "jsr:@std/http@^1.0.9",
"@std/testing": "jsr:@std/testing@^1.0.4",
"https://deno.land/x/oauth2_client/": "./"
},
"tasks": {
"check:types": "deno check **/*.ts",
"test": "deno test --parallel --trace-leaks --doc"
}
},
"exports": "./mod.ts"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means @cmd-johnson/deno-oauth2-client is the only entry point. Alternatively, we could also include more specific modules. E.g.:

  • @cmd-johnson/deno-oauth2-client/errors
  • @cmd-johnson/deno-oauth2-client/pkce
  • @cmd-johnson/deno-oauth2-client/refresh-token-grant

}
392 changes: 390 additions & 2 deletions deno.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions examples/http.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {
Cookie,
type Cookie,
deleteCookie,
getCookies,
setCookie,
} from "https://deno.land/[email protected]/http/cookie.ts";
import { OAuth2Client } from "https://deno.land/x/oauth2_client/mod.ts";
} from "@std/http/cookie";
import { OAuth2Client } from "@cmd-johnson/oauth2-client";

const oauth2Client = new OAuth2Client({
clientId: Deno.env.get("CLIENT_ID")!,
Expand Down
6 changes: 3 additions & 3 deletions examples/oak.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Application, Router } from "https://deno.land/x/[email protected]/mod.ts";
import { Application, Router } from "@oak/oak";
import {
MemoryStore,
Session,
} from "https://deno.land/x/oak_sessions@v4.0.5/mod.ts";
import { OAuth2Client } from "https://deno.land/x/oauth2_client/mod.ts";
} from "https://deno.land/x/oak_sessions@v9.0.0/mod.ts";
import { OAuth2Client } from "@cmd-johnson/oauth2-client";

const oauth2Client = new OAuth2Client({
clientId: Deno.env.get("CLIENT_ID")!,
Expand Down
4 changes: 2 additions & 2 deletions src/authorization_code_grant_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import {
assertMatch,
assertNotMatch,
assertRejects,
} from "https://deno.land/[email protected]/assert/mod.ts";
} from "@std/assert";
import {
assertSpyCall,
assertSpyCallAsync,
assertSpyCalls,
spy,
} from "https://deno.land/[email protected]/testing/mock.ts";
} from "@std/testing/mock";

import {
AuthorizationResponseError,
Expand Down
2 changes: 1 addition & 1 deletion src/client_credentials_grant.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { MissingClientSecretError } from "./errors.ts";
import { OAuth2GrantBase } from "./grant_base.ts";
import type { OAuth2Client } from "./oauth2_client.ts";
import { RequestOptions, Tokens } from "./types.ts";
import type { RequestOptions, Tokens } from "./types.ts";

export interface ClientCredentialsTokenOptions {
/**
Expand Down
2 changes: 1 addition & 1 deletion src/client_credentials_grant_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
assertMatch,
assertNotMatch,
assertRejects,
} from "https://deno.land/[email protected]/assert/mod.ts";
} from "@std/assert";

import {
MissingClientSecretError,
Expand Down
4 changes: 3 additions & 1 deletion src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ export class OAuth2ResponseError extends Error {
this.state = response.state;
}

public static fromURLSearchParams(params: URLSearchParams) {
public static fromURLSearchParams(
params: URLSearchParams,
): OAuth2ResponseError {
const error = params.get("error");
if (error === null) {
throw new TypeError("error URL parameter must be set");
Expand Down
5 changes: 1 addition & 4 deletions src/errors_test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
assertEquals,
assertThrows,
} from "https://deno.land/[email protected]/assert/mod.ts";
import { assertEquals, assertThrows } from "@std/assert";
import {
AuthorizationResponseError,
OAuth2ResponseError,
Expand Down
4 changes: 2 additions & 2 deletions src/grant_base.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { OAuth2ResponseError, TokenResponseError } from "./errors.ts";
import { OAuth2Client } from "./oauth2_client.ts";
import { RequestOptions, Tokens } from "./types.ts";
import type { OAuth2Client } from "./oauth2_client.ts";
import type { RequestOptions, Tokens } from "./types.ts";

interface AccessTokenResponse {
"access_token": string;
Expand Down
7 changes: 3 additions & 4 deletions src/grant_base_test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { assertEquals } from "https://deno.land/[email protected]/assert/assert_equals.ts";

import { OAuth2Client, OAuth2ClientConfig } from "./oauth2_client.ts";
import { assertEquals } from "@std/assert/equals";
import { OAuth2Client, type OAuth2ClientConfig } from "./oauth2_client.ts";
import { OAuth2GrantBase } from "./grant_base.ts";
import { RequestOptions } from "./types.ts";
import type { RequestOptions } from "./types.ts";

class OAuth2Grant extends OAuth2GrantBase {
public override buildRequest(
Expand Down
7 changes: 2 additions & 5 deletions src/implicit_grant_test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
// deno-lint-ignore-file no-explicit-any
import {
assertEquals,
assertRejects,
} from "https://deno.land/[email protected]/assert/mod.ts";
import { assertEquals, assertRejects } from "@std/assert";
import {
assertSpyCall,
assertSpyCallAsync,
assertSpyCalls,
spy,
} from "https://deno.land/[email protected]/testing/mock.ts";
} from "@std/testing/mock";

import { AuthorizationResponseError, OAuth2ResponseError } from "./errors.ts";
import {
Expand Down
15 changes: 9 additions & 6 deletions src/oauth2_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ClientCredentialsGrant } from "./client_credentials_grant.ts";
import { ImplicitGrant } from "./implicit_grant.ts";
import { RefreshTokenGrant } from "./refresh_token_grant.ts";
import { ResourceOwnerPasswordCredentialsGrant } from "./resource_owner_password_credentials.ts";
import { RequestOptions } from "./types.ts";
import type { RequestOptions } from "./types.ts";

export interface OAuth2ClientConfig {
/** The client ID provided by the authorization server. */
Expand Down Expand Up @@ -38,35 +38,38 @@ export class OAuth2Client {
*
* See RFC6749, section 4.1.
*/
public code = new AuthorizationCodeGrant(this);
public code: AuthorizationCodeGrant = new AuthorizationCodeGrant(this);

/**
* Implements the Implicit Grant.
*
* See RFC6749, section 4.2.
*/
public implicit = new ImplicitGrant(this);
public implicit: ImplicitGrant = new ImplicitGrant(this);

/**
* Implements the Resource Owner Password Credentials Grant.
*
* See RFC6749, section 4.3.
*/
public ropc = new ResourceOwnerPasswordCredentialsGrant(this);
public ropc: ResourceOwnerPasswordCredentialsGrant =
new ResourceOwnerPasswordCredentialsGrant(this);

/**
* Implements the Resource Owner Password Credentials Grant.
*
* See RFC6749, section 4.4.
*/
public clientCredentials = new ClientCredentialsGrant(this);
public clientCredentials: ClientCredentialsGrant = new ClientCredentialsGrant(
this,
);

/**
* Implements the Refresh Token Grant.
*
* See RFC6749, section 6.
*/
public refreshToken = new RefreshTokenGrant(this);
public refreshToken: RefreshTokenGrant = new RefreshTokenGrant(this);

constructor(
public readonly config: Readonly<OAuth2ClientConfig>,
Expand Down
2 changes: 1 addition & 1 deletion src/oauth2_client_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assert } from "https://deno.land/[email protected]/assert/assert.ts";
import { assert } from "@std/assert/assert";

import { OAuth2Client } from "./oauth2_client.ts";
import { AuthorizationCodeGrant } from "./authorization_code_grant.ts";
Expand Down
2 changes: 1 addition & 1 deletion src/pkce.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { encodeBase64 } from "https://deno.land/[email protected]/encoding/base64.ts";
import { encodeBase64 } from "@std/encoding/base64";

export interface PkceChallenge {
codeVerifier: string;
Expand Down
10 changes: 2 additions & 8 deletions src/pkce_test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import {
assertEquals,
assertMatch,
} from "https://deno.land/[email protected]/assert/mod.ts";
import {
returnsNext,
stub,
} from "https://deno.land/[email protected]/testing/mock.ts";
import { assertEquals, assertMatch } from "@std/assert";
import { returnsNext, stub } from "@std/testing/mock";

import { _internals as pkceInternals, createPkceChallenge } from "./pkce.ts";

Expand Down
4 changes: 2 additions & 2 deletions src/refresh_token_grant.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RequestOptions, Tokens } from "./types.ts";
import { OAuth2Client } from "./oauth2_client.ts";
import type { RequestOptions, Tokens } from "./types.ts";
import type { OAuth2Client } from "./oauth2_client.ts";
import { OAuth2GrantBase } from "./grant_base.ts";

export interface RefreshTokenOptions {
Expand Down
2 changes: 1 addition & 1 deletion src/refresh_token_grant_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertEquals } from "https://deno.land/[email protected]/assert/assert_equals.ts";
import { assertEquals } from "@std/assert/equals";
import { getOAuth2Client, mockATResponse } from "./test_utils.ts";

Deno.test("RefreshTokenGrant.refresh works without optional options", async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/resource_owner_password_credentials_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
assertMatch,
assertNotMatch,
assertRejects,
} from "https://deno.land/[email protected]/assert/mod.ts";
} from "@std/assert";

import { OAuth2ResponseError, TokenResponseError } from "./errors.ts";
import { getOAuth2Client, mockATResponse } from "./test_utils.ts";
Expand Down
11 changes: 4 additions & 7 deletions src/test_utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
// deno-lint-ignore-file camelcase

import { assertEquals } from "https://deno.land/[email protected]/assert/assert_equals.ts";
import {
returnsNext,
stub,
} from "https://deno.land/[email protected]/testing/mock.ts";
import { OAuth2Client, OAuth2ClientConfig } from "./oauth2_client.ts";
import { Tokens } from "./types.ts";
import { assertEquals } from "@std/assert/equals";
import { returnsNext, stub } from "@std/testing/mock";
import { OAuth2Client, type OAuth2ClientConfig } from "./oauth2_client.ts";
import type { Tokens } from "./types.ts";

export function getOAuth2Client(
overrideConfig: Partial<OAuth2ClientConfig> = {},
Expand Down