/
mastodon.js
151 lines (136 loc) · 4.45 KB
/
mastodon.js
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
class Mastodon {
constructor(options) {
/* options are: app_name, app_url, scopes */
this.options = options;
}
static async initialize(options) {
const mastodon = new Mastodon(options);
mastodon.redirectUri = window.location.origin + window.location.pathname;
mastodon.restore();
await mastodon.get_access_code();
return mastodon;
}
restore() {
if (localStorage.getItem(this.options.app_name)) {
const data = JSON.parse(localStorage.getItem(this.options.app_name));
this.clientId = data.clientId;
this.clientSecret = data.clientSecret;
this.instanceURL = data.instanceURL;
this.accessToken = data.accessToken;
}
}
save() {
localStorage.setItem(this.options.app_name, JSON.stringify({
clientId: this.clientId,
clientSecret: this.clientSecret,
instanceURL: this.instanceURL,
accessToken: this.accessToken,
}));
}
logout() {
localStorage.removeItem(this.options.app_name);
this.clientId = undefined;
this.clientSecret = undefined;
this.instanceURL = undefined;
this.accessToken = undefined;
}
async login(instanceURL) {
if (this.accessToken) {
return;
}
this.instanceURL = instanceURL;
await this.register_application();
window.location = this.generate_auth_link();
}
loggedIn() {
return this.accessToken !== undefined;
}
async get_access_code() {
if (window.location.search.length > 0) {
const params = new URLSearchParams(window.location.search);
if (params.has('code')) {
const accessCode = params.get('code');
await this.get_access_token(accessCode);
window.history.replaceState({}, document.title, window.location.pathname);
}
}
}
async register_application() {
console.log('logging in');
if (this.accessToken) {
console.log('already logged in');
return;
}
const url = `${this.instanceURL}/api/v1/apps`;
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
client_name: this.options.app_name,
redirect_uris: this.redirectUri,
scopes: this.options.scopes,
website: this.options.app_url,
}),
});
const data = await response.json();
this.clientId = data.client_id;
this.clientSecret = data.client_secret;
this.save()
return data;
}
async get_access_token(accessCode) {
const response = await fetch(`${this.instanceURL}/oauth/token`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
client_id: this.clientId,
client_secret: this.clientSecret,
code: accessCode,
redirect_uri: this.redirectUri,
grant_type: 'authorization_code',
scope: this.options.scopes,
}
)});
const data = await response.json();
this.accessToken = data.access_token;
this.save();
}
generate_auth_link() {
const params = new URLSearchParams({
client_id: this.clientId,
redirect_uri: this.redirectUri,
response_type: 'code',
scope: this.options.scopes,
});
return `${this.instanceURL}/oauth/authorize?${params}`;
}
normalize(path) {
if (path.startsWith('/')) {
return `${this.instanceURL}${path}`;
}
return path;
}
async post(path, data) {
const response = await fetch(this.normalize(path), {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.accessToken}`,
},
body: JSON.stringify(data),
});
return response;
}
async get(path) {
const response = await fetch(this.normalize(path), {
headers: {
'Authorization': `Bearer ${this.accessToken}`,
},
});
return response;
}
}