From c3ec804956f54ce3e0b091aaa7da0d6bbc4e3e3a Mon Sep 17 00:00:00 2001 From: Nicolas Constant Date: Fri, 16 Mar 2018 23:52:10 -0400 Subject: [PATCH] First iteration of auth service --- Mamoth/Mamoth.njsproj | 3 ++ Mamoth/src/app/app.module.ts | 29 ++++++----- .../register-new-account.component.html | 20 ++++++-- .../register-new-account.component.ts | 27 +++++++++-- Mamoth/src/app/services/auth.service.ts | 48 +++++++++++++++++++ .../src/app/services/models/api.settings.ts | 47 ++++++++++++++++++ 6 files changed, 153 insertions(+), 21 deletions(-) create mode 100644 Mamoth/src/app/services/auth.service.ts create mode 100644 Mamoth/src/app/services/models/api.settings.ts diff --git a/Mamoth/Mamoth.njsproj b/Mamoth/Mamoth.njsproj index ea4e121d..c453540a 100644 --- a/Mamoth/Mamoth.njsproj +++ b/Mamoth/Mamoth.njsproj @@ -81,6 +81,7 @@ + @@ -106,6 +107,8 @@ Code + + diff --git a/Mamoth/src/app/app.module.ts b/Mamoth/src/app/app.module.ts index 71a5e40f..be9f0c6a 100644 --- a/Mamoth/src/app/app.module.ts +++ b/Mamoth/src/app/app.module.ts @@ -1,16 +1,19 @@ -import { BrowserModule } from '@angular/platform-browser'; -import { NgModule } from '@angular/core'; +import { BrowserModule } from "@angular/platform-browser"; +import { FormsModule } from "@angular/forms"; +import { HttpModule } from "@angular/http"; +import { NgModule } from "@angular/core"; import { RouterModule, Routes } from "@angular/router"; -import { NgxElectronModule } from 'ngx-electron'; +import { NgxElectronModule } from "ngx-electron"; -import { AppComponent } from './app.component'; -import { LeftSideBarComponent } from './components/left-side-bar/left-side-bar.component'; -import { StreamsMainDisplayComponent } from './pages/streams-main-display/streams-main-display.component'; -import { StreamComponent } from './components/stream/stream.component'; -import { StreamsSelectionFooterComponent } from './components/streams-selection-footer/streams-selection-footer.component'; -import { TootComponent } from './components/toot/toot.component'; -import { RegisterNewAccountComponent } from './pages/register-new-account/register-new-account.component'; +import { AppComponent } from "./app.component"; +import { LeftSideBarComponent } from "./components/left-side-bar/left-side-bar.component"; +import { StreamsMainDisplayComponent } from "./pages/streams-main-display/streams-main-display.component"; +import { StreamComponent } from "./components/stream/stream.component"; +import { StreamsSelectionFooterComponent } from "./components/streams-selection-footer/streams-selection-footer.component"; +import { TootComponent } from "./components/toot/toot.component"; +import { RegisterNewAccountComponent } from "./pages/register-new-account/register-new-account.component"; +import { AuthService } from "./services/auth.service"; const routes: Routes = [ { path: "", redirectTo: "home", pathMatch: "full" }, @@ -30,11 +33,13 @@ const routes: Routes = [ RegisterNewAccountComponent ], imports: [ - BrowserModule, + BrowserModule, + HttpModule, + FormsModule, NgxElectronModule, RouterModule.forRoot(routes) ], - providers: [], + providers: [AuthService], bootstrap: [AppComponent] }) export class AppModule { } diff --git a/Mamoth/src/app/pages/register-new-account/register-new-account.component.html b/Mamoth/src/app/pages/register-new-account/register-new-account.component.html index b0065912..f2082003 100644 --- a/Mamoth/src/app/pages/register-new-account/register-new-account.component.html +++ b/Mamoth/src/app/pages/register-new-account/register-new-account.component.html @@ -1,6 +1,18 @@ + +
+ +
+ +
+ +
+ +
+

- register-new-account works! - - close - + Result:
+ {{ result }}

+ + +close diff --git a/Mamoth/src/app/pages/register-new-account/register-new-account.component.ts b/Mamoth/src/app/pages/register-new-account/register-new-account.component.ts index 5cfca7c3..e24de39f 100644 --- a/Mamoth/src/app/pages/register-new-account/register-new-account.component.ts +++ b/Mamoth/src/app/pages/register-new-account/register-new-account.component.ts @@ -1,15 +1,32 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, Input } from "@angular/core"; +import { AuthService } from "../../services/auth.service"; @Component({ - selector: 'app-register-new-account', - templateUrl: './register-new-account.component.html', - styleUrls: ['./register-new-account.component.css'] + selector: "app-register-new-account", + templateUrl: "./register-new-account.component.html", + styleUrls: ["./register-new-account.component.css"] }) export class RegisterNewAccountComponent implements OnInit { + @Input() mastodonNode: string; + @Input() email: string; + @Input() password: string; + result: string; - constructor() { } + constructor( + private readonly authService: AuthService) { } ngOnInit() { } + onSubmit(): boolean { + this.authService.getToken(this.mastodonNode, this.email, this.password) + .then((res: string) => { + this.result = res; + }) + .catch(err => { + this.result = err; + }); + + return false; + } } diff --git a/Mamoth/src/app/services/auth.service.ts b/Mamoth/src/app/services/auth.service.ts new file mode 100644 index 00000000..58fb2afa --- /dev/null +++ b/Mamoth/src/app/services/auth.service.ts @@ -0,0 +1,48 @@ +import { Injectable } from "@angular/core"; +import { Http, Response, RequestOptions } from "@angular/http"; +import { ApiRoutes } from "./models/api.settings"; + +@Injectable() +export class AuthService { + private apiRoutes = new ApiRoutes(); + + constructor( + private readonly httpService: Http) { + } + + getToken( + mastodonNode: string, email: string, password: string): Promise { + + //TODO retrieve those via API + const clientId = localStorage.getItem("client_id"); + const clientSecret = localStorage.getItem("client_secret"); + + //Retrieve Token + const url = this.getHostUrl(mastodonNode) + this.apiRoutes.getToken; + + const options = new RequestOptions(); + const formData = new FormData(); + + formData.append("client_id", clientId); + formData.append("client_secret", clientSecret); + formData.append("grant_type", "password"); + formData.append("username", email); + formData.append("password", password); + formData.append("scope", "read write follow"); + + return this.httpService.post(url, formData, options).toPromise() + .then((res: Response) => { + const result = res.json(); + console.warn(result); + return result; + }); + } + + private getHostUrl(url: string): string { + url = url.replace("http://", ""); + if (!url.startsWith("https://")) { + url = "https://" + url; + } + return url; + } +} diff --git a/Mamoth/src/app/services/models/api.settings.ts b/Mamoth/src/app/services/models/api.settings.ts new file mode 100644 index 00000000..171a9e26 --- /dev/null +++ b/Mamoth/src/app/services/models/api.settings.ts @@ -0,0 +1,47 @@ + +export class ApiRoutes { + createApp = "/api/v1/apps"; + getToken = "/oauth/token"; + getAccount = "/api/v1/accounts/{0}"; + getCurrentAccount = "/api/v1/accounts/verify_credentials"; + getAccountFollowers = "/api/v1/accounts/{0}/followers"; + getAccountFollowing = "/api/v1/accounts/{0}/following"; + getAccountStatuses = "/api/v1/accounts/{0}/statuses"; + follow = "/api/v1/accounts/{0}/follow"; + unfollow = "/api/v1/accounts/{0}/unfollow"; + block = "/api/v1/accounts/{0}/block"; + unblock = "/api/v1/accounts/{0}/unblock"; + mute = "/api/v1/accounts/{0}/mute"; + unmute = "/api/v1/accounts/{0}/unmute"; + getAccountRelationships = "/api/v1/accounts/relationships"; + searchForAccounts = "/api/v1/accounts/search"; + getBlocks = "/api/v1/blocks"; + getFavourites = "/api/v1/favourites"; + getFollowRequests = "/api/v1/follow_requests"; + authorizeFollowRequest = "/api/v1/follow_requests/authorize"; + rejectFollowRequest = "/api/v1/follow_requests/reject"; + followRemote = "/api/v1/follows"; + getInstance = "/api/v1/instance"; + uploadMediaAttachment = "/api/v1/media"; + getMutes = "/api/v1/mutes"; + getNotifications = "/api/v1/notifications"; + getSingleNotifications = "/api/v1/notifications/{0}"; + clearNotifications = "/api/v1/notifications/clear"; + getReports = "/api/v1/reports"; + reportUser = "/api/v1/reports"; + search = "/api/v1/search"; + getStatus = "/api/v1/statuses/{0}"; + getStatusContext = "/api/v1/statuses/{0}/context"; + getStatusCard = "/api/v1/statuses/{0}/card"; + getStatusRebloggedBy = "/api/v1/statuses/{0}/reblogged_by"; + getStatusFavouritedBy = "/api/v1/statuses/{0}/favourited_by"; + postNewStatus = "/api/v1/statuses"; + deleteStatus = "/api/v1/statuses/{0}"; + reblogStatus = "/api/v1/statuses/{0}/reblog"; + unreblogStatus = "/api/v1/statuses/{0}/unreblog"; + favouritingStatus = "/api/v1/statuses/{0}/favourite"; + unfavouritingStatus = "/api/v1/statuses/{0}/unfavourite"; + getHomeTimeline = "/api/v1/timelines/home"; + getPublicTimeline = "/api/v1/timelines/public"; + getHastagTimeline = "/api/v1/timelines/tag/{0}"; +}