Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tidusjar committed Dec 22, 2022
1 parent 2e4b8fe commit 4c5354c
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 96 deletions.
222 changes: 136 additions & 86 deletions src/Ombi/ClientApp/src/app/auth/auth.service.ts
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');
}
}
10 changes: 5 additions & 5 deletions src/Ombi/ClientApp/src/app/state/sonarr/sonarr.state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { SonarrState, SONARR_STATE_TOKEN } from "./types";
import { SettingsService, SonarrService } from "../../services";
import { AuthService } from "../../auth/auth.service";
import { Injectable } from "@angular/core";
import { combineLatest, Observable, of } from "rxjs";
import { map, tap } from "rxjs/operators";
import { combineLatest, firstValueFrom, Observable, of } from "rxjs";
import { map, take, tap } from "rxjs/operators";
import { LoadSettings, UpdateSettings } from "./sonarr.actions";
import { ISonarrSettings } from "../../interfaces";

Expand All @@ -14,11 +14,11 @@ import { ISonarrSettings } from "../../interfaces";
})
@Injectable()
export class SonarrSettingsState {
constructor(private sonarrService: SonarrService, private settingsService: SettingsService, private authService: AuthService) { }
constructor(private sonarrService: SonarrService, private settingsService: SettingsService, private authService: AuthService) { }

@Action(LoadSettings)
public load({ setState }: StateContext<SonarrState>): Observable<SonarrState> {
const isAdmin = this.authService.isAdmin();
const isAdmin = this.authService.isAdminSync();
const calls = isAdmin ? [this.sonarrService.getVersion(), this.settingsService.getSonarr()] : [of(""), of({})];

return combineLatest(calls).pipe(
Expand All @@ -35,7 +35,7 @@ export class SonarrSettingsState {
const state = ctx.getState();
return this.settingsService.saveSonarr(settings).pipe(
tap((_) => ctx.setState({...state, settings})),
map(_ => <SonarrState>{...state, settings})
map((_) => <SonarrState>{...state, settings})
);
}
}
2 changes: 1 addition & 1 deletion src/Ombi/ClientApp/src/app/wizard/emby/emby.component.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="mediaserver-container">
<div class="left-container mediaserver">
<img src="https://emby.media/resources/logowhite_1881.png">
<img src="{{baseUrl}}/images/emby-logo.png">
</div>
<div class="right-container mediaserver">
<div class="right-container-content mediaserver">
Expand Down
10 changes: 9 additions & 1 deletion src/Ombi/ClientApp/src/app/wizard/emby/emby.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { EmbyService } from "../../services";
import { IEmbySettings } from "../../interfaces";
import { NotificationService } from "../../services";
import { PlatformLocation } from "@angular/common";

@Component({
selector: "wizard-emby",
Expand All @@ -12,9 +13,11 @@ import { NotificationService } from "../../services";
export class EmbyComponent implements OnInit {

public embySettings: IEmbySettings;
public baseUrl: string;

constructor(private embyService: EmbyService,
private notificationService: NotificationService) {
private notificationService: NotificationService,
private location: PlatformLocation) {
}

public ngOnInit() {
Expand All @@ -37,6 +40,11 @@ export class EmbyComponent implements OnInit {
serverId: undefined,
embySelectedLibraries: []
});

const base = this.location.getBaseHrefFromDOM();
if (base.length > 1) {
this.baseUrl = base;
}
}

public save() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="mediaserver-container">
<div class="left-container mediaserver">
<img src="https://jellyfin.org/images/banner-dark.svg">
<img src="{{baseUrl}}/images/jellyfin.svg">
</div>
<div class="right-container mediaserver">
<div class="right-container-content mediaserver">
Expand Down
12 changes: 10 additions & 2 deletions src/Ombi/ClientApp/src/app/wizard/jellyfin/jellyfin.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Component, OnInit } from "@angular/core";
import { PlatformLocation } from "@angular/common";
import { Component, OnInit } from "@angular/core";

import { IJellyfinSettings } from "../../interfaces";
import { JellyfinService } from "../../services";
Expand All @@ -12,9 +13,11 @@ import { NotificationService } from "../../services";
export class JellyfinComponent implements OnInit {

public jellyfinSettings: IJellyfinSettings;
public baseUrl: string;

constructor(private jellyfinService: JellyfinService,
private notificationService: NotificationService) {
private notificationService: NotificationService,
private location: PlatformLocation) {
}

public ngOnInit() {
Expand All @@ -37,6 +40,11 @@ export class JellyfinComponent implements OnInit {
serverId: undefined,
jellyfinSelectedLibraries: []
});

const base = this.location.getBaseHrefFromDOM();
if (base.length > 1) {
this.baseUrl = base;
}
}

public save() {
Expand Down
34 changes: 34 additions & 0 deletions src/Ombi/wwwroot/images/jellyfin.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4c5354c

Please sign in to comment.