-
Notifications
You must be signed in to change notification settings - Fork 4
/
oauth2.ts
129 lines (116 loc) · 3.38 KB
/
oauth2.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
import {
AuthorizationCodeGrant,
AuthorizationServer,
AuthorizeParameters,
ClientCredentialsGrant,
loginRedirectFactory,
OakAuthorizationServer,
OakOAuth2AuthorizeRequest,
OakOAuth2Request,
OakOAuth2Response,
RefreshTokenGrant,
Router,
Scope,
} from "./deps.ts";
import { Client } from "./models/client.ts";
import { Session } from "./models/session.ts";
import { User } from "./models/user.ts";
import {
authorizationCodeService,
clientService,
sessionService,
tokenService,
} from "./services/mod.ts";
const services = {
clientService,
authorizationCodeService,
tokenService,
};
const authorizationCodeGrant = new AuthorizationCodeGrant({
services,
allowRefreshToken: true,
});
const clientCredentialsGrant = new ClientCredentialsGrant({ services });
const refreshTokenGrant = new RefreshTokenGrant({ services });
const oauth2Server = new AuthorizationServer({
grants: {
"authorization_code": authorizationCodeGrant,
"client_credentials": clientCredentialsGrant,
"refresh_token": refreshTokenGrant,
},
services,
});
async function getSession(
request: OakOAuth2Request<Client, User, Scope>,
): Promise<Session | undefined> {
const sessionId: string | undefined = await request.cookies.get("sessionId");
const session: Session | undefined = sessionId
? await sessionService.get(sessionId)
: undefined;
if (!session && sessionId) request.cookies.delete("sessionId");
return session;
}
export const oauth2 = new OakAuthorizationServer({
server: oauth2Server,
async getAccessToken(
request: OakOAuth2Request<Client, User, Scope>,
): Promise<string | null> {
const accessToken = await request.cookies.get("accessToken");
return accessToken ?? null;
},
});
export const oauth2Router = new Router();
oauth2Router.post("/token", oauth2.token());
const setAuthorization = async (
request: OakOAuth2AuthorizeRequest<Client, User, Scope>,
): Promise<void> => {
if (request.method === "POST") {
const body: URLSearchParams | undefined = await request.body;
const authorizedScopeText = body?.get("authorized_scope") ?? undefined;
if (authorizedScopeText) {
request.authorizedScope = new Scope(authorizedScopeText);
}
}
const { clientId } = request.authorizeParameters;
const token = await oauth2.getTokenForRequest(request)
.catch(() => undefined);
if (token) {
request.user = token.user;
}
if (!request.user && clientId === "1000") {
const session = await getSession(request);
if (session?.user) {
request.user = session.user;
}
}
};
const login = loginRedirectFactory<Client, User, Scope>({ loginUrl: "/login" });
const consentPage = (
authorizeParameters: AuthorizeParameters,
) => `
<html>
<head>
<title>Login</title>
</head>
<body>
<form method="post">
<input type="text" readonly name="authorized_scope" value="${
authorizeParameters
.scope ?? ""
}"/>
<input type="submit" value="Consent"/>
</form>
</body>
</html>
`;
const consent = async (
request: OakOAuth2AuthorizeRequest<Client, User, Scope>,
response: OakOAuth2Response,
): Promise<void> => {
response.body = 401;
response.body = consentPage(request.authorizeParameters);
return await Promise.resolve();
};
const authorize = oauth2.authorize(setAuthorization, login, consent);
oauth2Router.get("/authorize", authorize);
oauth2Router.post("/authorize", authorize);