From 4e5447158a5d6177a4456451310d5b94dfe51920 Mon Sep 17 00:00:00 2001 From: Aditya <60684641+0x0elliot@users.noreply.github.com> Date: Thu, 10 Aug 2023 19:46:27 +0530 Subject: [PATCH 1/3] fix: oauth refresh token refresh function --- oauth2.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/oauth2.go b/oauth2.go index 8f4230d..6ebd1c4 100755 --- a/oauth2.go +++ b/oauth2.go @@ -2941,6 +2941,52 @@ func MakeGmailWebhookRequest(ctx context.Context, webhookUrl string, mappedData return nil } +func RefreshOutlookClient(ctx context.Context, auth TriggerAuth) (error) { + // Manually recreate the oauthtoken + conf := &oauth2.Config{ + ClientID: os.Getenv("OUTLOOK_CLIENT_ID"), + ClientSecret: os.Getenv("OUTLOOK_CLIENT_SECRET"), + Scopes: []string{ + "Mail.Read", + }, + Endpoint: oauth2.Endpoint{ + TokenURL: "https://login.microsoftonline.com/common/oauth2/token", + }, + } + + // save new access_token, expiry, refresh_token to database + trigger, err := GetTriggerAuth(ctx, auth.Id) + if err != nil { + log.Printf("[WARNING] Failed getting trigger auth for outlook: %s", err) + return err + } + + tokenSource := conf.TokenSource(ctx, &oauth2.Token{ + AccessToken: auth.OauthToken.AccessToken, + RefreshToken: auth.OauthToken.RefreshToken, + Expiry: auth.OauthToken.Expiry, + TokenType: auth.OauthToken.TokenType, + }) + + token, err := tokenSource.Token() + if err != nil { + log.Printf("[WARNING] Failed getting token for outlook: %s", err) + return err + } + + trigger.OauthToken.AccessToken = token.AccessToken + trigger.OauthToken.RefreshToken = token.RefreshToken + trigger.OauthToken.Expiry = token.Expiry + + err = SetTriggerAuth(ctx, trigger) + if err != nil { + log.Printf("[WARNING] Failed setting trigger auth for outlook: %s", err) + return err + } + + return nil +} + func RefreshGmailClient(ctx context.Context, auth TriggerAuth) (*http.Client, error) { // Manually recreate the oauthtoken conf := &oauth2.Config{ From 56384d53f383955fbbb6e088ea6c5f6957531a92 Mon Sep 17 00:00:00 2001 From: Aditya <60684641+0x0elliot@users.noreply.github.com> Date: Fri, 11 Aug 2023 14:55:10 +0530 Subject: [PATCH 2/3] fix: fixing oauth2 refresh token function for once --- oauth2.go | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/oauth2.go b/oauth2.go index 6ebd1c4..49f1605 100755 --- a/oauth2.go +++ b/oauth2.go @@ -14,6 +14,7 @@ import ( "log" "net/http" "strconv" + //"net/url" "os" "strings" @@ -822,6 +823,7 @@ func HandleNewOutlookRegister(resp http.ResponseWriter, request *http.Request) { Expiry: accessToken.Expiry, } + //log.Printf("%#v", trigger) log.Println(trigger.WorkflowId) log.Println(trigger.Id) @@ -2944,7 +2946,7 @@ func MakeGmailWebhookRequest(ctx context.Context, webhookUrl string, mappedData func RefreshOutlookClient(ctx context.Context, auth TriggerAuth) (error) { // Manually recreate the oauthtoken conf := &oauth2.Config{ - ClientID: os.Getenv("OUTLOOK_CLIENT_ID"), + ClientID: os.Getenv("OUTLOOK_CLIENT_ID"), ClientSecret: os.Getenv("OUTLOOK_CLIENT_SECRET"), Scopes: []string{ "Mail.Read", @@ -2961,29 +2963,29 @@ func RefreshOutlookClient(ctx context.Context, auth TriggerAuth) (error) { return err } - tokenSource := conf.TokenSource(ctx, &oauth2.Token{ - AccessToken: auth.OauthToken.AccessToken, + token, err := conf.TokenSource(ctx, &oauth2.Token{ RefreshToken: auth.OauthToken.RefreshToken, - Expiry: auth.OauthToken.Expiry, - TokenType: auth.OauthToken.TokenType, - }) + }).Token() - token, err := tokenSource.Token() if err != nil { log.Printf("[WARNING] Failed getting token for outlook: %s", err) return err } + log.Printf("[INFO] Token %s refreshed successfully from outlook. Proceeding to save..", auth.Id) + trigger.OauthToken.AccessToken = token.AccessToken trigger.OauthToken.RefreshToken = token.RefreshToken trigger.OauthToken.Expiry = token.Expiry - err = SetTriggerAuth(ctx, trigger) + err = SetTriggerAuth(ctx, *trigger) if err != nil { log.Printf("[WARNING] Failed setting trigger auth for outlook: %s", err) return err } + log.Printf("[INFO] Successfully refreshed outlook token for trigger %s and user %s", auth.Id, auth.Owner) + return nil } @@ -3912,4 +3914,4 @@ func VerifyIdToken(ctx context.Context, idToken string) (IdTokenCheck, error) { } return IdTokenCheck{}, errors.New("Couldn't verify nonce") -} +} \ No newline at end of file From 6355fd5707ccfb9b076f0bf57de0bccea8f1c20f Mon Sep 17 00:00:00 2001 From: Aditya <60684641+0x0elliot@users.noreply.github.com> Date: Fri, 11 Aug 2023 16:26:36 +0530 Subject: [PATCH 3/3] fix: matching up environment variables --- oauth2.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/oauth2.go b/oauth2.go index 49f1605..9a1291c 100755 --- a/oauth2.go +++ b/oauth2.go @@ -2946,8 +2946,8 @@ func MakeGmailWebhookRequest(ctx context.Context, webhookUrl string, mappedData func RefreshOutlookClient(ctx context.Context, auth TriggerAuth) (error) { // Manually recreate the oauthtoken conf := &oauth2.Config{ - ClientID: os.Getenv("OUTLOOK_CLIENT_ID"), - ClientSecret: os.Getenv("OUTLOOK_CLIENT_SECRET"), + ClientID: os.Getenv("OFFICE365_CLIENT_ID"), + ClientSecret: os.Getenv("OFFICE365_CLIENT_SECRET"), Scopes: []string{ "Mail.Read", },