Skip to content

Commit

Permalink
code refactor AuthCredentials
Browse files Browse the repository at this point in the history
  • Loading branch information
marijanlekic committed Feb 25, 2019
1 parent 53d20e1 commit cef0c29
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 31 deletions.
43 changes: 20 additions & 23 deletions src/app/auth/model/auth-credentials.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import {User} from "../../../../electron/src/sbg-api-client/interfaces/user";
import {UserPlatformIdentifier} from "./user-platform-identifier";

interface PlatformEntry {
name: string;
shortName: string;
devTokenURL: string;
}

export class AuthCredentials implements UserPlatformIdentifier {

static readonly URL_VALIDATION_REGEXP = "^(https:\/\/)(.+)(\.sbgenomics\.com)$";

static readonly TOKEN_VALIDATION_REGEXP = "^[0-9a-f]{8}[0-9a-f]{4}[0-9a-f]{4}[0-9a-f]{4}[0-9a-f]{12}$";

static readonly platformLookupByAPIURL = {
static readonly platformLookupByAPIURL: { [key: string]: PlatformEntry } = {
"https://api.sbgenomics.com": {
"name": "Seven Bridges",
"shortName": "SBG",
Expand Down Expand Up @@ -38,7 +44,7 @@ export class AuthCredentials implements UserPlatformIdentifier {
"shortName": "F4C",
"devTokenURL": "https://f4c.sbgenomics.com/developer#token",
},
}
};

id: string;
user: User;
Expand Down Expand Up @@ -68,31 +74,12 @@ export class AuthCredentials implements UserPlatformIdentifier {
return url.slice(8, url.length - 15);
}

static getPlatformList(): Array<Map<string, string>> {
let lu = this.platformLookupByAPIURL
let platformList = []
for (let apiURL in lu) {
platformList.push({text: lu[apiURL]["name"], value: apiURL})
}
return platformList
}

static getPlatformShortName(url: string): string {
if(url in this.platformLookupByAPIURL) {
return this.platformLookupByAPIURL[url]["shortName"]
} else {
const subdomain = AuthCredentials.getSubdomain(url)
return subdomain.indexOf("vayu") === -1 ? subdomain : subdomain.split(".")[0]
}
return AuthCredentials.getPlatformPropertyValue(url, "shortName");
}

static getPlatformLabel(url: string): string {
if(url in this.platformLookupByAPIURL) {
return this.platformLookupByAPIURL[url]["name"]
} else {
const subdomain = AuthCredentials.getSubdomain(url)
return subdomain.indexOf("vayu") === -1 ? subdomain : subdomain.split(".")[0]
}
return AuthCredentials.getPlatformPropertyValue(url, "name");
}

static from(obj?: UserPlatformIdentifier): AuthCredentials | undefined {
Expand Down Expand Up @@ -160,4 +147,14 @@ export class AuthCredentials implements UserPlatformIdentifier {
throw new Error("Invalid platform URL: " + url);
}
}

private static getPlatformPropertyValue(url: string, property: keyof PlatformEntry): string {

const platform = this.platformLookupByAPIURL[url];
const subdomain = AuthCredentials.getSubdomain(url);

return platform ? platform[property]
: (subdomain.indexOf("vayu") === -1 ? subdomain : subdomain.split(".")[0]);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,22 @@ export class PlatformCredentialsModalComponent implements OnInit {
/** FormGroup for modal inputs */
form: FormGroup;

platformList = AuthCredentials.getPlatformList()

platformList = [];

constructor(private system: SystemService,
private auth: AuthService,
private global: GlobalService,
private data: DataGatewayService,
private notificationBarService: NotificationBarService,
private modal: ModalService) {

const platformLookupByAPIURL = AuthCredentials.platformLookupByAPIURL;

this.platformList = Object.keys(platformLookupByAPIURL).map((item) => {
return {
text: platformLookupByAPIURL[item].name, value: item
}
});
}

applyChanges(): void {
Expand Down Expand Up @@ -282,13 +289,14 @@ export class PlatformCredentialsModalComponent implements OnInit {
}

openTokenPage() {

const apiURL: string = this.form.get("url").value;
if(apiURL in AuthCredentials.platformLookupByAPIURL) {
this.system.openLink(AuthCredentials.platformLookupByAPIURL[apiURL]["devTokenURL"])
} else {
// Most likely a vayu
this.system.openLink("https://igor.sbgenomics.com/developer#token")
}

const platform = AuthCredentials.platformLookupByAPIURL[apiURL];
const url = platform ? platform.devTokenURL : "https://igor.sbgenomics.com/developer#token";

this.system.openLink(url);

}

close() {
Expand Down

0 comments on commit cef0c29

Please sign in to comment.