-
-
Notifications
You must be signed in to change notification settings - Fork 404
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
196 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,92 +1,142 @@ | ||
import { PlatformLocation, APP_BASE_HREF } from "@angular/common"; | ||
import { HttpClient } from "@angular/common/http"; | ||
import { Injectable, Inject } from "@angular/core"; | ||
import { JwtHelperService } from "@auth0/angular-jwt"; | ||
import { defer, firstValueFrom, from, map, Observable, take } from "rxjs"; | ||
import { PlatformLocation, APP_BASE_HREF } from '@angular/common'; | ||
import { HttpClient } from '@angular/common/http'; | ||
import { Injectable, Inject } from '@angular/core'; | ||
import { JwtHelperService } from '@auth0/angular-jwt'; | ||
import { defer, firstValueFrom, from, map, Observable, take } from 'rxjs'; | ||
|
||
import { ServiceHelpers } from "../services"; | ||
import { ILocalUser, IUserLogin } from "./IUserLogin"; | ||
import { StorageService } from "../shared/storage/storage-service"; | ||
import { ServiceHelpers } from '../services'; | ||
import { ILocalUser, IUserLogin } from './IUserLogin'; | ||
import { StorageService } from '../shared/storage/storage-service'; | ||
|
||
@Injectable() | ||
export class AuthService extends ServiceHelpers { | ||
constructor(http: HttpClient, @Inject(APP_BASE_HREF) href: string, private jwtHelperService: JwtHelperService, private store: StorageService) { | ||
super(http, '/api/v1/token', href); | ||
} | ||
|
||
constructor(http: HttpClient, @Inject(APP_BASE_HREF) href: string, private jwtHelperService: JwtHelperService, | ||
private store: StorageService) { | ||
super(http, "/api/v1/token", href); | ||
} | ||
|
||
public login(login: IUserLogin): Observable<any> { | ||
return this.http.post(`${this.url}/`, JSON.stringify(login), { headers: this.headers }); | ||
} | ||
|
||
public oAuth(pin: number): Observable<any> { | ||
return this.http.get<any>(`${this.url}/${pin}`, { headers: this.headers }); | ||
} | ||
|
||
public requiresPassword(login: IUserLogin): Observable<boolean> { | ||
return this.http.post<boolean>(`${this.url}/requirePassword`, JSON.stringify(login), { headers: this.headers }); | ||
} | ||
|
||
public headerAuth(): Observable<any> { | ||
return this.http.post<boolean>(`${this.url}/header_auth`, {}, { headers: this.headers }); | ||
} | ||
|
||
public getToken() { | ||
return this.jwtHelperService.tokenGetter(); | ||
} | ||
|
||
public loggedIn(): Observable<boolean> { | ||
return from(this.jwtHelperService.tokenGetter()). | ||
pipe(take(1), | ||
map(token => { | ||
if (!token) { | ||
return false; | ||
} | ||
|
||
const tokenExpired: boolean = this.jwtHelperService.isTokenExpired(token); | ||
return !tokenExpired; | ||
})); | ||
} | ||
|
||
public claims(): Observable<ILocalUser> { | ||
return this.loggedIn().pipe(take(1), map(x => { | ||
if (x) { | ||
const token = this.store.get("id_token"); | ||
if (!token) { | ||
throw new Error("Invalid token"); | ||
} | ||
const json = this.jwtHelperService.decodeToken(token); | ||
const roles = json.role; | ||
const name = json.sub; | ||
const email = json.Email; | ||
|
||
const u = { name, roles: [] as string[], email }; | ||
if (roles instanceof Array) { | ||
u.roles = roles; | ||
} else { | ||
u.roles.push(roles); | ||
} | ||
return <ILocalUser>u; | ||
} | ||
return <ILocalUser>{}; | ||
})); | ||
} | ||
|
||
public hasRole(role: string): Observable<boolean> { | ||
return this.claims().pipe(take(1), map(claims => { | ||
if (claims && claims.roles && role && claims.roles.length > 0) { | ||
return claims.roles.some(r => r != undefined && r.toUpperCase() === role.toUpperCase()); | ||
} | ||
return false; | ||
})); | ||
} | ||
|
||
public isAdmin(): Observable<boolean> { | ||
return this.hasRole("Admin") || this.hasRole("PowerUser"); | ||
} | ||
|
||
public logout() { | ||
this.store.remove("id_token"); | ||
} | ||
public login(login: IUserLogin): Observable<any> { | ||
return this.http.post(`${this.url}/`, JSON.stringify(login), { headers: this.headers }); | ||
} | ||
|
||
public oAuth(pin: number): Observable<any> { | ||
return this.http.get<any>(`${this.url}/${pin}`, { headers: this.headers }); | ||
} | ||
|
||
public requiresPassword(login: IUserLogin): Observable<boolean> { | ||
return this.http.post<boolean>(`${this.url}/requirePassword`, JSON.stringify(login), { headers: this.headers }); | ||
} | ||
|
||
public headerAuth(): Observable<any> { | ||
return this.http.post<boolean>(`${this.url}/header_auth`, {}, { headers: this.headers }); | ||
} | ||
|
||
public getToken() { | ||
return this.jwtHelperService.tokenGetter(); | ||
} | ||
|
||
public loggedIn(): Observable<boolean> { | ||
return from(this.jwtHelperService.tokenGetter()).pipe( | ||
take(1), | ||
map((token) => { | ||
if (!token) { | ||
return false; | ||
} | ||
|
||
const tokenExpired: boolean = this.jwtHelperService.isTokenExpired(token); | ||
return !tokenExpired; | ||
}), | ||
); | ||
} | ||
|
||
public loggedInSync(): boolean { | ||
const token = this.jwtHelperService.tokenGetter() as string; | ||
if (!token) { | ||
return false; | ||
} | ||
|
||
const tokenExpired: boolean = this.jwtHelperService.isTokenExpired(token); | ||
return !tokenExpired; | ||
} | ||
|
||
public claims(): Observable<ILocalUser> { | ||
return this.loggedIn().pipe( | ||
take(1), | ||
map((x) => { | ||
if (x) { | ||
const token = this.store.get('id_token'); | ||
if (!token) { | ||
throw new Error('Invalid token'); | ||
} | ||
const json = this.jwtHelperService.decodeToken(token); | ||
const roles = json.role; | ||
const name = json.sub; | ||
const email = json.Email; | ||
|
||
const u = { name, roles: [] as string[], email }; | ||
if (roles instanceof Array) { | ||
u.roles = roles; | ||
} else { | ||
u.roles.push(roles); | ||
} | ||
return <ILocalUser>u; | ||
} | ||
return <ILocalUser>{}; | ||
}), | ||
); | ||
} | ||
|
||
public claimsSync(): ILocalUser { | ||
const x = this.loggedInSync(); | ||
if (x) { | ||
const token = this.store.get('id_token'); | ||
if (!token) { | ||
throw new Error('Invalid token'); | ||
} | ||
const json = this.jwtHelperService.decodeToken(token); | ||
const roles = json.role; | ||
const name = json.sub; | ||
const email = json.Email; | ||
|
||
const u = { name, roles: [] as string[], email }; | ||
if (roles instanceof Array) { | ||
u.roles = roles; | ||
} else { | ||
u.roles.push(roles); | ||
} | ||
return <ILocalUser>u; | ||
} | ||
return <ILocalUser>{}; | ||
} | ||
|
||
public hasRole(role: string): Observable<boolean> { | ||
return this.claims().pipe( | ||
take(1), | ||
map((claims) => { | ||
if (claims && claims.roles && role && claims.roles.length > 0) { | ||
return claims.roles.some((r) => r != undefined && r.toUpperCase() === role.toUpperCase()); | ||
} | ||
return false; | ||
}), | ||
); | ||
} | ||
|
||
public hasRoleSync(role: string): boolean { | ||
const claims = this.claimsSync(); | ||
if (claims && claims.roles && role && claims.roles.length > 0) { | ||
return claims.roles.some((r) => r != undefined && r.toUpperCase() === role.toUpperCase()); | ||
} | ||
return false; | ||
} | ||
|
||
public isAdmin(): Observable<boolean> { | ||
return this.hasRole('Admin') || this.hasRole('PowerUser'); | ||
} | ||
|
||
public isAdminSync(): boolean { | ||
return this.hasRoleSync('Admin') || this.hasRoleSync('PowerUser'); | ||
} | ||
|
||
public logout() { | ||
this.store.remove('id_token'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/Ombi/ClientApp/src/app/wizard/jellyfin/jellyfin.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.