-
Notifications
You must be signed in to change notification settings - Fork 4
/
context.ts
219 lines (202 loc) · 6.21 KB
/
context.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import { AuthorizationCode } from "./models/authorization_code.ts";
import { ClientInterface } from "./models/client.ts";
import { ScopeInterface } from "./models/scope.ts";
import { Token } from "./models/token.ts";
export interface OAuth2Request<
Client extends ClientInterface,
User,
Scope extends ScopeInterface,
> {
url: URL;
headers: Headers;
method: string;
body: Promise<URLSearchParams>;
token?: Token<Client, User, Scope> | null;
accessToken?: string | null;
authorizationCode?: AuthorizationCode<Client, User, Scope> | null;
authorizeParameters?: AuthorizeParameters;
authorizedScope?: Scope;
redirectUrl?: URL;
user?: User;
acceptedScope?: Scope;
requestedScope?: Scope;
}
export interface OAuth2AuthenticatedRequest<
Client extends ClientInterface,
User,
Scope extends ScopeInterface,
> extends OAuth2Request<Client, User, Scope> {
token: Token<Client, User, Scope>;
}
export interface OAuth2AuthorizeRequest<
Client extends ClientInterface,
User,
Scope extends ScopeInterface,
> extends OAuth2Request<Client, User, Scope> {
authorizeParameters: AuthorizeParameters;
}
export interface OAuth2AuthorizedRequest<
Client extends ClientInterface,
User,
Scope extends ScopeInterface,
> extends OAuth2AuthorizeRequest<Client, User, Scope> {
authorizationCode: AuthorizationCode<Client, User, Scope>;
redirectUrl: URL;
}
export interface OAuth2Response {
status?: number;
headers: Headers;
// deno-lint-ignore no-explicit-any
body?: any | Promise<any> | (() => (any | Promise<any>));
redirect(url: string | URL): Promise<void>;
}
export interface TokenBody {
"token_type": string;
"access_token": string;
"expires_in"?: number;
"refresh_token"?: string;
scope?: string;
}
export interface ErrorBody {
error: string;
"error_description"?: string;
"error_uri"?: string;
}
export interface AuthorizeParameters {
responseType: string | null;
clientId: string | null;
redirectUri: string | null;
state: string | null;
scope: string | null;
challenge: string | null;
challengeMethod: string | null;
}
/** Gets the authorize parameters from the request. */
export async function authorizeParameters<
Client extends ClientInterface,
User,
Scope extends ScopeInterface,
>(
request: OAuth2Request<Client, User, Scope>,
): Promise<AuthorizeParameters> {
let responseType: string | null = null;
let clientId: string | null = null;
let redirectUri: string | null = null;
let state: string | null = null;
let scope: string | null = null;
let challenge: string | null = null;
let challengeMethod: string | null = null;
if (request.method === "POST") {
const contentType: string | null = request.headers.get("content-type");
if (
contentType === "application/x-www-form-urlencoded"
) {
const body: URLSearchParams = await request.body!;
responseType = body.get("response_type");
clientId = body.get("client_id");
redirectUri = body.get("redirect_uri");
state = body.get("state");
scope = body.get("scope");
challenge = body.get("code_challenge");
challengeMethod = body.get("code_challenge_method");
}
}
const url: URL = request.url;
const { searchParams } = url;
if (!responseType) responseType = searchParams.get("response_type");
if (!clientId) clientId = searchParams.get("client_id");
if (!redirectUri) redirectUri = searchParams.get("redirect_uri");
if (!state) state = searchParams.get("state");
if (!scope) scope = searchParams.get("scope");
if (!challenge) challenge = searchParams.get("code_challenge");
if (!challengeMethod) {
challengeMethod = searchParams.get("code_challenge_method");
}
return {
responseType,
clientId,
redirectUri,
state,
scope,
challenge,
challengeMethod,
};
}
/** Generates url for an authorization get request. */
export function authorizeUrl<
Client extends ClientInterface,
User,
Scope extends ScopeInterface,
>(
request: OAuth2AuthorizeRequest<Client, User, Scope>,
): URL {
const getUrl: URL = new URL(request.url.toString());
getUrl.search = "";
const { searchParams } = getUrl;
const {
responseType,
clientId,
redirectUri,
state,
scope,
challenge,
challengeMethod,
} = request.authorizeParameters;
if (responseType) searchParams.set("response_type", responseType);
if (clientId) searchParams.set("client_id", clientId);
if (redirectUri) searchParams.set("redirect_uri", redirectUri);
if (state) searchParams.set("state", state);
if (scope) searchParams.set("scope", scope);
if (challenge) searchParams.set("code_challenge", challenge);
if (challengeMethod) {
searchParams.set("code_challenge_method", challengeMethod);
}
return getUrl;
}
export interface LoginRedirectOptions {
/** The url for logging in when unauthenticated. */
loginUrl: string | URL;
/** The search parameter key for the url to redirect to after logging in. Defaults to "redirect_url". */
loginRedirectKey?: string;
}
/** Used for redirecting to login page for the authorization code flow. */
export function loginRedirectFactory<
Client extends ClientInterface,
User,
Scope extends ScopeInterface,
>(
options: LoginRedirectOptions,
) {
const { loginUrl } = options;
const loginRedirectKey = options.loginRedirectKey ?? "redirect_uri";
return async (
request: OAuth2AuthorizeRequest<Client, User, Scope>,
response: OAuth2Response,
) => {
if (typeof loginUrl === "string") {
const queryIndex: number = loginUrl.indexOf("?");
let pathname: string;
let searchParams: URLSearchParams;
if (queryIndex === -1) {
pathname = loginUrl;
searchParams = new URLSearchParams();
} else {
pathname = loginUrl.slice(0, queryIndex);
searchParams = new URLSearchParams(loginUrl.slice(queryIndex));
}
searchParams.set(
loginRedirectKey,
authorizeUrl(request).toString(),
);
response.redirect(`${pathname}?${searchParams.toString()}`);
} else {
const target = new URL(loginUrl.toString());
const searchParams = target.searchParams;
searchParams.set(
loginRedirectKey,
authorizeUrl(request).toString(),
);
await response.redirect(target);
}
};
}