Skip to content

Commit

Permalink
fix: fixing oauth2 refresh token function for once
Browse files Browse the repository at this point in the history
  • Loading branch information
0x0elliot committed Aug 11, 2023
1 parent 4e54471 commit 56384d5
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"log"
"net/http"
"strconv"

//"net/url"
"os"
"strings"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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",
Expand All @@ -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
}

Expand Down Expand Up @@ -3912,4 +3914,4 @@ func VerifyIdToken(ctx context.Context, idToken string) (IdTokenCheck, error) {
}

return IdTokenCheck{}, errors.New("Couldn't verify nonce")
}
}

0 comments on commit 56384d5

Please sign in to comment.