11import * as ko from "knockout" ;
22import * as validation from "knockout.validation" ;
33import template from "./operation-console.html" ;
4+ import { HttpClient , HttpRequest , HttpResponse } from "@paperbits/common/http" ;
45import { Component , Param , OnMounted } from "@paperbits/common/ko/decorators" ;
56import { ISettingsProvider } from "@paperbits/common/configuration" ;
67import { Operation } from "../../../../../models/operation" ;
@@ -16,7 +17,6 @@ import { ProductService } from "../../../../../services/productService";
1617import { UsersService } from "../../../../../services/usersService" ;
1718import { TenantService } from "../../../../../services/tenantService" ;
1819import { ServiceSkuName , TypeOfApi } from "../../../../../constants" ;
19- import { HttpClient , HttpRequest , HttpResponse } from "@paperbits/common/http" ;
2020import { Revision } from "../../../../../models/revision" ;
2121import { templates } from "./templates/templates" ;
2222import { ConsoleParameter } from "../../../../../models/console/consoleParameter" ;
@@ -27,6 +27,8 @@ import { OAuthService } from "../../../../../services/oauthService";
2727import { AuthorizationServer } from "../../../../../models/authorizationServer" ;
2828import { SessionManager } from "../../../../../authentication/sessionManager" ;
2929import { OAuthSession , StoredCredentials } from "./oauthSession" ;
30+ import { UnauthorizedError } from "../../../../../errors/unauthorizedError" ;
31+ import { GrantTypes } from "./../../../../../constants" ;
3032import { ResponsePackage } from "./responsePackage" ;
3133
3234const oauthSessionKey = "oauthSession" ;
@@ -56,6 +58,10 @@ export class OperationConsole {
5658 public readonly hostnameSelectionEnabled : ko . Observable < boolean > ;
5759 public readonly wildcardSegment : ko . Observable < string > ;
5860 public readonly selectedGrantType : ko . Observable < string > ;
61+ public readonly username : ko . Observable < string > ;
62+ public readonly password : ko . Observable < string > ;
63+ public readonly authorizationError : ko . Observable < string > ;
64+ public readonly authenticated : ko . Observable < boolean > ;
5965 public isConsumptionMode : boolean ;
6066 public templates : Object ;
6167 public backendUrl : string ;
@@ -98,6 +104,11 @@ export class OperationConsole {
98104 this . isHostnameWildcarded = ko . computed ( ( ) => this . selectedHostname ( ) . includes ( "*" ) ) ;
99105 this . selectedGrantType = ko . observable ( ) ;
100106 this . authorizationServer = ko . observable ( ) ;
107+ this . username = ko . observable ( ) ;
108+ this . password = ko . observable ( ) ;
109+ this . authorizationError = ko . observable ( ) ;
110+ this . authenticated = ko . observable ( false ) ;
111+
101112 this . useCorsProxy = ko . observable ( false ) ;
102113 this . wildcardSegment = ko . observable ( ) ;
103114
@@ -153,7 +164,7 @@ export class OperationConsole {
153164 this . api . subscribe ( this . resetConsole ) ;
154165 this . operation . subscribe ( this . resetConsole ) ;
155166 this . selectedLanguage . subscribe ( this . updateRequestSummary ) ;
156- this . selectedGrantType . subscribe ( this . authenticateOAuth ) ;
167+ this . selectedGrantType . subscribe ( this . onGrantTypeChange ) ;
157168 }
158169
159170 private async resetConsole ( ) : Promise < void > {
@@ -374,6 +385,7 @@ export class OperationConsole {
374385 private removeAuthorizationHeader ( ) : void {
375386 const authorizationHeader = this . findHeader ( KnownHttpHeaders . Authorization ) ;
376387 this . removeHeader ( authorizationHeader ) ;
388+ this . authenticated ( false ) ;
377389 }
378390
379391 private setAuthorizationHeader ( accessToken : string ) : void {
@@ -390,6 +402,7 @@ export class OperationConsole {
390402
391403 this . consoleOperation ( ) . request . headers . push ( keyHeader ) ;
392404 this . updateRequestSummary ( ) ;
405+ this . authenticated ( true ) ;
393406 }
394407
395408 private removeSubscriptionKeyHeader ( ) : void {
@@ -625,27 +638,53 @@ export class OperationConsole {
625638 this . removeAuthorizationHeader ( ) ;
626639 }
627640
628- /**
629- * Initiates specified authentication flow.
630- * @param grantType OAuth grant type, e.g. "implicit" or "authorization_code".
631- */
632- public async authenticateOAuth ( grantType : string ) : Promise < void > {
641+ public async authenticateOAuthWithPassword ( ) : Promise < void > {
642+ try {
643+ this . authorizationError ( null ) ;
644+
645+ const api = this . api ( ) ;
646+ const authorizationServer = this . authorizationServer ( ) ;
647+ const scopeOverride = api . authenticationSettings ?. oAuth2 ?. scope ;
648+ const serverName = authorizationServer . name ;
649+
650+ if ( scopeOverride ) {
651+ authorizationServer . scopes = [ scopeOverride ] ;
652+ }
653+
654+ const accessToken = await this . oauthService . authenticatePassword ( this . username ( ) , this . password ( ) , authorizationServer ) ;
655+ await this . setStoredCredentials ( serverName , scopeOverride , GrantTypes . password , accessToken ) ;
656+
657+ this . setAuthorizationHeader ( accessToken ) ;
658+ }
659+ catch ( error ) {
660+ if ( error instanceof UnauthorizedError ) {
661+ this . authorizationError ( error . message ) ;
662+ return ;
663+ }
664+
665+ this . authorizationError ( "Oops, something went wrong. Try again later." ) ;
666+ }
667+ }
668+
669+ private async onGrantTypeChange ( grantType : string ) : Promise < void > {
633670 await this . clearStoredCredentials ( ) ;
634671
635- if ( ! grantType ) {
672+ if ( ! grantType || grantType === GrantTypes . password ) {
636673 return ;
637674 }
638675
676+ await this . authenticateOAuth ( grantType ) ;
677+ }
678+
679+ /**
680+ * Initiates specified authentication flow.
681+ * @param grantType OAuth grant type, e.g. "implicit" or "authorization_code".
682+ */
683+ public async authenticateOAuth ( grantType : string ) : Promise < void > {
639684 const api = this . api ( ) ;
640685 const authorizationServer = this . authorizationServer ( ) ;
641686 const scopeOverride = api . authenticationSettings ?. oAuth2 ?. scope ;
642687 const serverName = authorizationServer . name ;
643- const storedCredentials = await this . getStoredCredentials ( serverName , scopeOverride ) ;
644-
645- if ( storedCredentials ) {
646- this . setAuthorizationHeader ( storedCredentials . accessToken ) ;
647- return ;
648- }
649688
650689 if ( scopeOverride ) {
651690 authorizationServer . scopes = [ scopeOverride ] ;
0 commit comments