From f845ae3716613cefc26831225f3c7ee0b9e54701 Mon Sep 17 00:00:00 2001 From: Jason Kulatunga Date: Wed, 14 Sep 2022 08:19:28 -0700 Subject: [PATCH] make sure we wait for the token to be stored before reloading the page. --- backend/pkg/config/config.go | 10 ++++ backend/pkg/web/handler/auth.go | 4 +- backend/pkg/web/middleware/require_auth.go | 2 +- config.yaml | 2 +- .../medical-sources.component.ts | 47 +++++-------------- 5 files changed, 25 insertions(+), 40 deletions(-) diff --git a/backend/pkg/config/config.go b/backend/pkg/config/config.go index 19faebac..342cfef0 100644 --- a/backend/pkg/config/config.go +++ b/backend/pkg/config/config.go @@ -66,5 +66,15 @@ func (c *configuration) ReadConfig(configFilePath string) error { log.Printf("Error merging config file: %s", err) return err } + return c.ValidateConfig() +} + +// This function ensures that required configuration keys (that must be manually set) are present +func (c *configuration) ValidateConfig() error { + + if !c.IsSet("web.jwt.encryptionkey") { + return errors.ConfigValidationError("`web.jwt.encryptionkey` configuration option must be set") + } + return nil } diff --git a/backend/pkg/web/handler/auth.go b/backend/pkg/web/handler/auth.go index aac203af..bd685a41 100644 --- a/backend/pkg/web/handler/auth.go +++ b/backend/pkg/web/handler/auth.go @@ -26,7 +26,7 @@ func AuthSignup(c *gin.Context) { } // return JWT - tokenString, err := auth.GenerateJWT(appConfig.GetString("web.jwt.encryptionKey"), user.Username) + tokenString, err := auth.GenerateJWT(appConfig.GetString("web.jwt.encryptionkey"), user.Username) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"success": false}) return @@ -58,7 +58,7 @@ func AuthSignin(c *gin.Context) { } // return JWT - tokenString, err := auth.GenerateJWT(appConfig.GetString("web.jwt.encryptionKey"), user.Username) + tokenString, err := auth.GenerateJWT(appConfig.GetString("web.jwt.encryptionkey"), user.Username) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": "an error occurred generating JWT token"}) return diff --git a/backend/pkg/web/middleware/require_auth.go b/backend/pkg/web/middleware/require_auth.go index 8f967758..f2288136 100644 --- a/backend/pkg/web/middleware/require_auth.go +++ b/backend/pkg/web/middleware/require_auth.go @@ -30,7 +30,7 @@ func RequireAuth() gin.HandlerFunc { c.Abort() return } - claim, err := auth.ValidateToken(appConfig.GetString("web.jwt.encryptionKey"), tokenString) + claim, err := auth.ValidateToken(appConfig.GetString("web.jwt.encryptionkey"), tokenString) if err != nil { c.JSON(http.StatusUnauthorized, gin.H{"success": false, "error": err.Error()}) c.Abort() diff --git a/config.yaml b/config.yaml index eb1dc5f0..043c0175 100644 --- a/config.yaml +++ b/config.yaml @@ -8,7 +8,7 @@ version: 1 web: jwt: # used to encrypt/validate JWT session key (used for authentication) - encryptionKey: 'changethissupersecretkey' + encryptionkey: 'changethissupersecretkey' listen: port: 9090 host: 0.0.0.0 diff --git a/frontend/src/app/pages/medical-sources/medical-sources.component.ts b/frontend/src/app/pages/medical-sources/medical-sources.component.ts index a3bcbb93..20d4fcb8 100644 --- a/frontend/src/app/pages/medical-sources/medical-sources.component.ts +++ b/frontend/src/app/pages/medical-sources/medical-sources.component.ts @@ -143,44 +143,19 @@ export class MedicalSourcesComponent implements OnInit { code_verifier: codeVerifier, } - this.fastenApi.createSource(sourceCredential).subscribe( (respData) => { - console.log("source credential create response:", respData) - }) + await this.fastenApi.createSource(sourceCredential).subscribe( + (respData) => { + console.log("source credential create response:", respData) + }, + (err) => {console.log(err)}, + () => { + delete this.status[sourceType] + //reload the current page after finishing connection + window.location.reload(); + } + ) - - - // console.log("STARTING--- FHIR.client(clientState)", clientState) - // const fhirClient = FHIR.client(clientState); - // - // console.log("STARTING--- client.request(Patient)") - // const patientResponse = await fhirClient.request("PatientAccess/v1/$userinfo") - // console.log(patientResponse) - - - - - - // // fetch userinfo response - // - // const response = await oauth.userInfoRequest(as, client, access_token) - // - // let challenges: oauth.WWWAuthenticateChallenge[] | undefined - // if ((challenges = oauth.parseWwwAuthenticateChallenges(response))) { - // for (const challenge of challenges) { - // console.log('challenge', challenge) - // } - // throw new Error() // Handle www-authenticate challenges as needed - // } - // - // const result = await oauth.processUserInfoResponse(as, client, sub, response) - // console.log('result', result) - - delete this.status[sourceType] - - //reload the current page after finishing connection - window.location.reload(); - }) });