Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating new SSO auth token for new user or tenant #72

Merged
merged 1 commit into from
Jul 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions common/src/maven/MavenCliProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import * as path from "path"

import * as fs from "fs-extra"
nblagoev marked this conversation as resolved.
Show resolved Hide resolved

import * as jwtDecode from "jwt-decode"

import { BaseEnvironment } from "../platform"
import { MavenInfo } from "../types"
Expand Down Expand Up @@ -39,7 +39,7 @@ export class MavenCliProxy {
this.writeTokenPom(tokenPom)

let token = fs.existsSync(tokenFile) ? this.readTokenFile(tokenFile) : null
if (!token || this.isExpired(token)) {
if (!token || this.isExpired(token) || this.isDiffUserOrTenant(token)) {
const command = `mvn vrealize:auth -P${this.mavenSettings.profile} -DoutputDir="${tokenFolder}" -N -e`
const cmdOptions = { cwd: tokenFolder }

Expand Down Expand Up @@ -144,4 +144,39 @@ export class MavenCliProxy {

return now > expirationDate
}

private isDiffUserOrTenant(token: { value: string; expirationDate: string }): boolean {
let decodedToken
try {
decodedToken = jwtDecode(token.value)
} catch (e) {
this.logger.warn(`Invalid local SSO authentication token format!`)
return true;
}

// token (stored locally) details
const tokenUserQualifier = decodedToken.prn // user@TENANT
if (!tokenUserQualifier) {
return true;
}
const tokenUsername = tokenUserQualifier.match(/.+?(?=@)/)
if (!tokenUsername) {
return true;
}
const tokenTenant = tokenUserQualifier.match(/(?<=@).+[^\s]/)
if (!tokenTenant) {
return true;
}
const tokenDomain = decodedToken.domain
if (!tokenDomain) {
return true;
}

// Maven active profile details
const vroUsername = this.environment.getVroUsername() // user@domain
const vroTenant = this.environment.getVroTenant()

return (`${tokenUsername[0]}@${tokenDomain}`.toUpperCase() != vroUsername.toUpperCase() ||
tokenTenant[0].toUpperCase() != vroTenant.toUpperCase());
}
}
16 changes: 16 additions & 0 deletions common/src/platform/BaseEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,20 @@ export abstract class BaseEnvironment {

return this.config.activeProfile.getOptional("vro.host", "")
}

getVroUsername(): string {
if (!this.config.hasActiveProfile()) {
return ""
}

return this.config.activeProfile.getOptional("vro.username", "")
}

getVroTenant(): string {
if (!this.config.hasActiveProfile()) {
return ""
}

return this.config.activeProfile.getOptional("vro.tenant", "")
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@
"fs-extra": "~7.0.0",
"glob": "^7.1.6",
"jsonc-parser": "^2.1.0",
"jwt-decode": "^2.2.0",
"lodash": "^4.17.15",
"micromatch": "^4.0.2",
"module-alias": "^2.2.2",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4141,6 +4141,11 @@ just-debounce@^1.0.0:
resolved "https://registry.yarnpkg.com/just-debounce/-/just-debounce-1.0.0.tgz#87fccfaeffc0b68cd19d55f6722943f929ea35ea"
integrity sha1-h/zPrv/AtozRnVX2cilD+SnqNeo=

jwt-decode@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/jwt-decode/-/jwt-decode-2.2.0.tgz#7d86bd56679f58ce6a84704a657dd392bba81a79"
integrity sha1-fYa9VmefWM5qhHBKZX3TkruoGnk=

keytar@*:
version "6.0.1"
resolved "https://registry.yarnpkg.com/keytar/-/keytar-6.0.1.tgz#996961abdebf300b2d34bb2eab6e42a8096b1ed8"
Expand Down