Sengi-Windows-MacOS-Linux/src/app/services/auth.service.ts

63 lines
2.6 KiB
TypeScript
Raw Normal View History

2018-03-17 04:52:10 +01:00
import { Injectable } from "@angular/core";
2019-09-28 03:55:40 +02:00
import { HttpClient, HttpHeaders } from "@angular/common/http";
2018-09-08 06:49:11 +02:00
import { ApiRoutes } from './models/api.settings';
import { AppData, TokenData } from "./models/mastodon.interfaces";
2018-03-17 04:52:10 +01:00
@Injectable()
2019-06-15 01:36:21 +02:00
export class AuthService {
2018-09-09 07:29:23 +02:00
private apiRoutes = new ApiRoutes();
2018-09-08 06:49:11 +02:00
2018-09-09 07:29:23 +02:00
constructor(
private readonly httpClient: HttpClient) {
}
2018-03-17 04:52:10 +01:00
getInstanceLoginUrl(instance: string, client_id: string, redirect_uri: string): string{
2019-06-15 01:36:21 +02:00
return `https://${instance}/oauth/authorize?scope=${encodeURIComponent('read write follow')}&response_type=code&redirect_uri=${encodeURIComponent(redirect_uri)}&client_id=${client_id}&force_login=true`;
}
2018-09-09 07:29:23 +02:00
getToken(instance: string, client_id: string, client_secret: string, code: string, redirect_uri: string): Promise<TokenData> {
2019-09-28 03:55:40 +02:00
const encodedRedirectUri = encodeURIComponent(redirect_uri);
const url = `https://${instance}/oauth/token?client_id=${client_id}&client_secret=${client_secret}&grant_type=authorization_code&code=${code}&redirect_uri=${encodedRedirectUri}`;
const bodyLoad = {
'client_id': client_id,
'client_secret': client_secret,
'grant_type': 'authorization_code',
'redirect_uri': redirect_uri,
'code': code
};
return this.httpClient.post<TokenData>(url, bodyLoad).toPromise();
2018-09-09 07:29:23 +02:00
}
2018-09-08 06:49:11 +02:00
2019-10-02 05:05:52 +02:00
refreshToken(instance: string, client_id: string, client_secret: string, refresh_token: string): Promise<TokenData> {
const url = `https://${instance}/oauth/token?client_id=${client_id}&client_secret=${client_secret}&grant_type=refresh_token&refresh_token=${refresh_token}&scope=${encodeURIComponent('read write follow')}`;
const bodyLoad = {
'client_id': client_id,
'client_secret': client_secret,
'grant_type': 'refresh_token',
'refresh_token': refresh_token,
'scope': 'read write follow'
};
return this.httpClient.post<TokenData>(url, bodyLoad).toPromise();
}
2018-09-09 07:29:23 +02:00
createNewApplication(instance: string, appName: string, redirectUrl: string, scopes: string, website: string): Promise<AppData> {
const url = 'https://' + instance + this.apiRoutes.createApp;
2018-09-08 06:49:11 +02:00
2019-09-28 03:55:40 +02:00
const formData = new FormData();
2018-09-09 07:29:23 +02:00
formData.append('client_name', appName);
formData.append('redirect_uris', redirectUrl);
formData.append('scopes', scopes);
formData.append('website', website);
2018-09-08 06:49:11 +02:00
2018-09-09 07:29:23 +02:00
return this.httpClient.post<AppData>(url, formData).toPromise();
}
2018-03-17 04:52:10 +01:00
}
export class CurrentAuthProcess {
constructor(public instance: string) { }
}