-
Notifications
You must be signed in to change notification settings - Fork 7
/
miauth.ts
159 lines (142 loc) · 3.52 KB
/
miauth.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
function join(...paths: string[]) {
return paths.join("/");
}
async function ky(url: string, options: any) {
const response = await fetch(url, options);
if (!response.ok) {
throw new Error(`HTTP error: ${response.statusText}`);
}
return response;
}
/**
* parametor of MiAuth url
*/
export interface UrlParam {
name: string;
callback?: string;
permission: Array<string>;
}
/**
* thow when incorrect format url parameter hand over MiAuth class
*/
class UrlParameterError extends Error {
constructor() {
// TODO: ここに気の利いたエラーを入れる
super("UrlParameterError");
}
}
class AuthenticationError extends Error {
constructor() {
// TODO: ここに気の利いたエラーを入れる
super("AuthenticationError");
}
}
export class MiAuth {
/**
* MiAuth constructor
*
* origin:
* your on the instance url
*
* param:
* url parameter. see UrlParam
*/
private origin: string;
private param: UrlParam;
private session: string;
constructor(origin: string, param: UrlParam, session: string) {
this.origin = origin;
this.param = param;
this.session = session;
}
/**
* return miauth authentication url
*/
public authUrl(): string {
const param = this.buildParam(this.param);
const requestUrl = new URL(join("miauth", this.session), this.origin);
requestUrl.search = param;
return requestUrl.toString();
}
/**
* return misskey api token
* when failed authentication, throw AuthenticationError
*/
public async getToken(): Promise<string> {
const url = new URL(
join("api", "miauth", this.session, "check"),
this.origin
);
const data: Record<string, unknown> = await fetch(url).then((res) =>
res.json()
);
const token = String(data.token);
if (typeof data.token === "undefined") {
throw new AuthenticationError();
}
return token;
}
/**
* build for parameter of miauth authentication url
* when you incorrect parameter, thow UrlParametorError,
* but, when callback parameter incorrect do not throw that error
*/
private buildParam(param: UrlParam): string {
const urlParam: Record<string, string> = {
name: param.name,
callback: param.callback as string,
permission: param.permission.join(","),
};
const convertdParam = new URLSearchParams();
Object.keys(urlParam).forEach((key) => {
convertdParam.set(key, String(urlParam[key]));
if (key != "callback") {
if (typeof urlParam[key] === "undefined") {
throw UrlParameterError;
}
} else {
if (typeof urlParam[key] === "undefined") {
convertdParam.delete("callback");
}
}
});
return convertdParam.toString();
}
}
/**
* this is MiAuth constructor light wrapper
*/
export function quickAuth(origin: string, param: UrlParam): MiAuth {
const session = crypto.randomUUID();
return new MiAuth(origin, param, session);
}
export const Permissions = [
"read:account",
"write:account",
"write:notes",
"read:blocks",
"write:blocks",
"read:drive",
"write:drive",
"read:favorites",
"write:favorites",
"read:following",
"write:following",
"read:messaging",
"write:messaging",
"read:mutes",
"write:mutes",
"read:notifications",
"write:notifications",
"read:reactions",
"write:reactions",
"write:votes",
"read:pages",
"write:pages",
"read:page-likes",
"write:page-likes",
"read:user-groups",
"write:user-groups",
"read:channels",
"write:channels",
];