Skip to content

Commit 86c5435

Browse files
authored
Refactor and remove requireRefresh from getAccessToken (#37)
* Update dependencies * Refactor and remove requireRefresh from getAccessToken
1 parent 8936c23 commit 86c5435

38 files changed

+904
-1024
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# OAuth2 Server
22

3-
[![version](https://img.shields.io/badge/release-0.11.1-success)](https://deno.land/x/oauth2_server@0.11.1)
4-
[![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/oauth2_server@0.11.1/authorization_server.ts)
3+
[![version](https://img.shields.io/badge/release-0.12.0-success)](https://deno.land/x/oauth2_server@0.12.0)
4+
[![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/oauth2_server@0.12.0/authorization_server.ts)
55
[![CI](https://github.com/udibo/oauth2_server/workflows/CI/badge.svg)](https://github.com/udibo/oauth2_server/actions?query=workflow%3ACI)
66
[![codecov](https://codecov.io/gh/udibo/oauth2_server/branch/main/graph/badge.svg?token=8Q7TSUFWUY)](https://codecov.io/gh/udibo/oauth2_server)
77
[![license](https://img.shields.io/github/license/udibo/oauth2_server)](https://github.com/udibo/oauth2_server/blob/master/LICENSE)
@@ -44,19 +44,19 @@ also acting as an authorization server.
4444

4545
```ts
4646
// Import from Deno's third party module registry
47-
import { ResourceServer } from "https://deno.land/x/oauth2_server@0.11.1/resource_server.ts";
47+
import { ResourceServer } from "https://deno.land/x/oauth2_server@0.12.0/resource_server.ts";
4848
// Import from GitHub
49-
import { ResourceServer } from "https://raw.githubusercontent.com/udibo/oauth2_server/0.11.1/resource_server.ts";
49+
import { ResourceServer } from "https://raw.githubusercontent.com/udibo/oauth2_server/0.12.0/resource_server.ts";
5050
```
5151

5252
The AuthorizationServer is an extension of the ResourceServer, adding methods
5353
used by the authorize and token endpoints.
5454

5555
```ts
5656
// Import from Deno's third party module registry
57-
import { AuthorizationServer } from "https://deno.land/x/oauth2_server@0.11.1/authorization_server.ts";
57+
import { AuthorizationServer } from "https://deno.land/x/oauth2_server@0.12.0/authorization_server.ts";
5858
// Import from GitHub
59-
import { AuthorizationServer } from "https://raw.githubusercontent.com/udibo/oauth2_server/0.11.1/authorization_server.ts";
59+
import { AuthorizationServer } from "https://raw.githubusercontent.com/udibo/oauth2_server/0.12.0/authorization_server.ts";
6060
```
6161

6262
## Usage
@@ -66,7 +66,7 @@ An example of how to use this module can be found
6666
but it should give you an idea of how to use this module.
6767

6868
See
69-
[deno docs](https://doc.deno.land/https/deno.land/x/oauth2_server@0.11.1/authorization_server.ts)
69+
[deno docs](https://doc.deno.land/https/deno.land/x/oauth2_server@0.12.0/authorization_server.ts)
7070
for more information.
7171

7272
### Grants

adapters/oak/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@ also acting as an authorization server.
2020

2121
```ts
2222
// Import from Deno's third party module registry
23-
import { OakResourceServer } from "https://deno.land/x/oauth2_server@0.11.1/adapters/oak/resource_server.ts";
23+
import { OakResourceServer } from "https://deno.land/x/oauth2_server@0.12.0/adapters/oak/resource_server.ts";
2424
// Import from GitHub
25-
import { OakResourceServer } from "https://raw.githubusercontent.com/udibo/oauth2_server/0.11.1/adapters/oak/resource_server.ts";
25+
import { OakResourceServer } from "https://raw.githubusercontent.com/udibo/oauth2_server/0.12.0/adapters/oak/resource_server.ts";
2626
```
2727

2828
The AuthorizationServer is an extension of the ResourceServer, adding methods
2929
used by the authorize and token endpoints.
3030

3131
```ts
3232
// Import from Deno's third party module registry
33-
import { OakAuthorizationServer } from "https://deno.land/x/oauth2_server@0.11.1/adapters/oak/authorization_server.ts";
33+
import { OakAuthorizationServer } from "https://deno.land/x/oauth2_server@0.12.0/adapters/oak/authorization_server.ts";
3434
// Import from GitHub
35-
import { OakAuthorizationServer } from "https://raw.githubusercontent.com/udibo/oauth2_server/0.11.1/adapters/oak/authorization_server.ts";
35+
import { OakAuthorizationServer } from "https://raw.githubusercontent.com/udibo/oauth2_server/0.12.0/adapters/oak/authorization_server.ts";
3636
```
3737

3838
## Usage
@@ -42,5 +42,5 @@ An example of how to use this adapter module can be found
4242
but it should give you an idea of how to use this module.
4343

4444
See
45-
[deno docs](https://doc.deno.land/https/deno.land/x/oauth2_server@0.11.1/adapters/oak/authorization_server.ts)
45+
[deno docs](https://doc.deno.land/https/deno.land/x/oauth2_server@0.12.0/adapters/oak/authorization_server.ts)
4646
for more information.

adapters/oak/context_test.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ import {
44
assertSpyCall,
55
assertSpyCalls,
66
assertStrictEquals,
7+
describe,
8+
it,
79
Spy,
810
spy,
9-
test,
10-
TestSuite,
1111
} from "../../test_deps.ts";
1212
import { BodyForm, Context, Request, Response } from "./deps.ts";
1313
import { OakOAuth2Request, OakOAuth2Response } from "./context.ts";
1414

15-
const requestTests = new TestSuite({ name: "OakOAuth2Request" });
15+
const requestTests = describe("OakOAuth2Request");
1616

17-
test(requestTests, "get", async () => {
17+
it(requestTests, "get", async () => {
1818
const expectedBody = new URLSearchParams();
1919
const original: Request = {
2020
url: new URL("https://example.com/resource/1"),
@@ -35,7 +35,7 @@ test(requestTests, "get", async () => {
3535
assertStrictEquals(await wrapped.body, expectedBody);
3636
});
3737

38-
test(requestTests, "post", async () => {
38+
it(requestTests, "post", async () => {
3939
const expectedBody: URLSearchParams = new URLSearchParams({
4040
grant_type: "client_credentials",
4141
});
@@ -58,7 +58,7 @@ test(requestTests, "post", async () => {
5858
assertStrictEquals(await wrapped.body, expectedBody);
5959
});
6060

61-
test(requestTests, "post with sync body error", async () => {
61+
it(requestTests, "post with sync body error", async () => {
6262
const original: Request = {
6363
url: new URL("https://example.com/token"),
6464
method: "POST",
@@ -77,7 +77,7 @@ test(requestTests, "post with sync body error", async () => {
7777
assertEquals(await wrapped.body, new URLSearchParams());
7878
});
7979

80-
test(requestTests, "post with async body error", async () => {
80+
it(requestTests, "post with async body error", async () => {
8181
const original: Request = {
8282
url: new URL("https://example.com/token"),
8383
method: "POST",
@@ -97,9 +97,9 @@ test(requestTests, "post with async body error", async () => {
9797
await assertRejects(() => wrapped.body, Error, "failed");
9898
});
9999

100-
const responseTests = new TestSuite({ name: "OakOAuth2Response" });
100+
const responseTests = describe("OakOAuth2Response");
101101

102-
test(responseTests, "redirect", () => {
102+
it(responseTests, "redirect", () => {
103103
const original: Response = {
104104
redirect: (_url: string | URL) => undefined,
105105
} as Response;
@@ -123,7 +123,7 @@ test(responseTests, "redirect", () => {
123123
assertSpyCalls(redirect, 2);
124124
});
125125

126-
test(responseTests, "without body", () => {
126+
it(responseTests, "without body", () => {
127127
const headers: Headers = new Headers({ "Content-Type": `application/json` });
128128
const original: Response = {
129129
status: 404,
@@ -149,7 +149,7 @@ test(responseTests, "without body", () => {
149149
assertStrictEquals(original.body, undefined);
150150
});
151151

152-
test(responseTests, "with sync body value", () => {
152+
it(responseTests, "with sync body value", () => {
153153
const headers: Headers = new Headers({ "Content-Type": `application/json` });
154154
const original: Response = {
155155
status: 200,
@@ -168,7 +168,7 @@ test(responseTests, "with sync body value", () => {
168168
assertStrictEquals(original.body, body);
169169
});
170170

171-
test(responseTests, "with async body value", () => {
171+
it(responseTests, "with async body value", () => {
172172
const headers: Headers = new Headers({ "Content-Type": `application/json` });
173173
const original: Response = {
174174
status: 200,
@@ -189,7 +189,7 @@ test(responseTests, "with async body value", () => {
189189
assertStrictEquals(result, body);
190190
});
191191

192-
test(responseTests, "with body function", () => {
192+
it(responseTests, "with body function", () => {
193193
const headers: Headers = new Headers({ "Content-Type": `application/json` });
194194
const original: Response = {
195195
status: 200,

adapters/oak/deps.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ export {
33
Cookies,
44
Request,
55
Response,
6-
} from "https://deno.land/x/oak@v10.1.0/mod.ts";
6+
} from "https://deno.land/x/oak@v10.5.1/mod.ts";
77
export type {
88
BodyForm,
99
Middleware,
10-
} from "https://deno.land/x/oak@v10.1.0/mod.ts";
10+
} from "https://deno.land/x/oak@v10.5.1/mod.ts";

adapters/oak/resource_server.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ export class OakResourceServer<
3737
private stateKey: string;
3838
private getAccessToken: (
3939
request: OakOAuth2Request<Client, User, Scope>,
40-
requireRefresh?: boolean,
4140
) => Promise<string | null>;
4241

4342
constructor(options: OakResourceServerOptions<Client, User, Scope>) {
@@ -59,16 +58,19 @@ export class OakResourceServer<
5958
return state;
6059
}
6160

61+
async getToken(accessToken: string): Promise<Token<Client, User, Scope>> {
62+
return await this.server.getToken(accessToken);
63+
}
64+
6265
async getTokenForRequest(
6366
request: OakOAuth2Request<Client, User, Scope>,
64-
): Promise<Token<Client, User, Scope> | undefined> {
65-
return await this.server.getTokenForRequest(request, this.getAccessToken)
66-
.catch(() => undefined);
67+
): Promise<Token<Client, User, Scope>> {
68+
return await this.server.getTokenForRequest(request, this.getAccessToken);
6769
}
6870

6971
async getTokenForContext(
7072
context: Context,
71-
): Promise<Token<Client, User, Scope> | undefined> {
73+
): Promise<Token<Client, User, Scope>> {
7274
const state = this.getState(context);
7375
const { request } = state;
7476
return await this.getTokenForRequest(request);

asserts.ts

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,25 @@ import {
44
assertSpyCall,
55
assertStrictEquals,
66
Spy,
7-
SpyCall,
8-
Stub,
97
} from "./test_deps.ts";
108
import { ClientInterface } from "./models/client.ts";
119
import { ScopeInterface } from "./models/scope.ts";
1210
import { Token } from "./models/token.ts";
1311
import { AuthorizationCode } from "./models/authorization_code.ts";
1412

1513
export function assertScope<Scope extends ScopeInterface>(
16-
actual: Scope | null | undefined,
14+
actual: unknown,
1715
expected: Scope | null | undefined,
1816
): void {
1917
try {
2018
if (expected && actual) {
21-
assert(expected.equals(actual));
19+
assert(expected.equals(actual as Scope));
2220
} else {
2321
assertEquals(actual, expected);
2422
}
2523
} catch {
2624
assertEquals(
27-
actual ? [...actual].sort() : actual,
25+
actual ? [...actual as Scope].sort() : actual,
2826
expected ? [...expected].sort() : expected,
2927
);
3028
}
@@ -35,14 +33,17 @@ function assertWithoutScope<
3533
User,
3634
Scope extends ScopeInterface,
3735
>(
38-
actual:
39-
| Partial<AuthorizationCode<Client, User, Scope>>
40-
| Partial<Token<Client, User, Scope>>,
36+
actual: unknown,
4137
expected:
4238
| Partial<AuthorizationCode<Client, User, Scope>>
4339
| Partial<Token<Client, User, Scope>>,
4440
): void {
45-
const actualWithoutScope = { ...actual };
41+
const actualWithoutScope = {
42+
...(actual as (
43+
| Partial<AuthorizationCode<Client, User, Scope>>
44+
| Partial<Token<Client, User, Scope>>
45+
)),
46+
};
4647
delete actualWithoutScope.scope;
4748
const expectedWithoutScope = { ...expected };
4849
delete expectedWithoutScope.scope;
@@ -54,15 +55,18 @@ export function assertToken<
5455
User,
5556
Scope extends ScopeInterface,
5657
>(
57-
actual: Partial<Token<Client, User, Scope>> | null | undefined,
58+
actual: unknown,
5859
expected: Partial<Token<Client, User, Scope>> | null | undefined,
5960
): void {
6061
assert(
6162
!!actual === !!expected,
6263
actual ? "did not expect token" : "expected token",
6364
);
6465
if (actual && expected) {
65-
assertScope(actual.scope, expected.scope);
66+
assertScope(
67+
(actual as Partial<Token<Client, User, Scope>>).scope,
68+
expected.scope,
69+
);
6670
assertWithoutScope(actual, expected);
6771
}
6872
}
@@ -72,7 +76,7 @@ export function assertAuthorizationCode<
7276
User,
7377
Scope extends ScopeInterface,
7478
>(
75-
actual: Partial<AuthorizationCode<Client, User, Scope>> | null | undefined,
79+
actual: unknown,
7680
expected: Partial<AuthorizationCode<Client, User, Scope>> | null | undefined,
7781
): void {
7882
assert(
@@ -82,7 +86,10 @@ export function assertAuthorizationCode<
8286
: "expected authorization code",
8387
);
8488
if (actual && expected) {
85-
assertScope(actual.scope, expected.scope);
89+
assertScope(
90+
(actual as Partial<AuthorizationCode<Client, User, Scope>>).scope,
91+
expected.scope,
92+
);
8693
assertWithoutScope(actual, expected);
8794
}
8895
}
@@ -92,18 +99,17 @@ export function assertClientUserScopeCall<
9299
User,
93100
Scope extends ScopeInterface,
94101
>(
95-
// deno-lint-ignore no-explicit-any
96-
spy: Spy<any> | Stub<any>,
102+
spy: Spy,
97103
callIndex: number,
98-
// deno-lint-ignore no-explicit-any
99-
self: any,
104+
self: unknown,
100105
client: Client,
101106
user: User,
102-
expectedScope?: Scope | null,
107+
scope?: Scope | null,
103108
): void {
104-
const call: SpyCall = assertSpyCall(spy, callIndex);
109+
assertSpyCall(spy, callIndex);
110+
const call = spy.calls[callIndex];
105111
assertStrictEquals(call.self, self);
106112
assertEquals(call.args.slice(0, 2), [client, user]);
107113
const actualScope: ScopeInterface | undefined = call.args[2];
108-
assertScope(actualScope, expectedScope);
114+
assertScope(actualScope, scope);
109115
}

asserts_test.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,16 @@ import {
33
assertScope,
44
assertToken,
55
} from "./asserts.ts";
6-
import { AssertionError, assertThrows, test, TestSuite } from "./test_deps.ts";
6+
import { AssertionError, assertThrows, describe, it } from "./test_deps.ts";
77
import { Client } from "./models/client.ts";
88
import { Scope } from "./models/scope.ts";
99
import { Token } from "./models/token.ts";
1010
import { AuthorizationCode } from "./models/authorization_code.ts";
1111
import { User } from "./models/user.ts";
1212

13-
const assertsTests: TestSuite<void> = new TestSuite({
14-
name: "asserts",
15-
});
13+
const assertsTests = describe("asserts");
1614

17-
test(assertsTests, "assertScope", () => {
15+
it(assertsTests, "assertScope", () => {
1816
assertScope(undefined, undefined);
1917
assertScope(new Scope(), new Scope());
2018
assertScope(new Scope("read"), new Scope("read"));
@@ -56,7 +54,7 @@ test(assertsTests, "assertScope", () => {
5654
const client: Client = { id: "1", grants: [] };
5755
const user: User = { username: "kyle" };
5856

59-
test(assertsTests, "assertToken", () => {
57+
it(assertsTests, "assertToken", () => {
6058
const expectedToken: Token<Client, User, Scope> = {
6159
accessToken: "x",
6260
client: { id: "1", grants: [] },
@@ -169,7 +167,7 @@ test(assertsTests, "assertToken", () => {
169167
);
170168
});
171169

172-
test(assertsTests, "assertAuthorizationCode", () => {
170+
it(assertsTests, "assertAuthorizationCode", () => {
173171
const expiresAt = new Date(Date.now() + 60000);
174172
const expectedAuthorizationCode: AuthorizationCode<Client, User, Scope> = {
175173
code: "x",

authorization_server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ export {
366366
UNICODECHARNOCRLF,
367367
UnsupportedGrantTypeError,
368368
UnsupportedResponseTypeError,
369+
UnsupportedTokenTypeError,
369370
VSCHAR,
370371
} from "./resource_server.ts";
371372
export type {
@@ -381,7 +382,7 @@ export type {
381382
OAuth2AuthenticatedRequest,
382383
OAuth2AuthorizedRequest,
383384
OAuth2AuthorizeRequest,
384-
OAuth2ErrorInit,
385+
OAuth2ErrorOptions,
385386
OAuth2Request,
386387
OAuth2Response,
387388
RefreshToken,

0 commit comments

Comments
 (0)