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

feat: added function to refresh token for outlook refresh token expiry bug #28

Merged
merged 3 commits into from
Aug 14, 2023
Merged
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
50 changes: 49 additions & 1 deletion 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 @@ -2941,6 +2943,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("OFFICE365_CLIENT_ID"),
ClientSecret: os.Getenv("OFFICE365_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
}

token, err := conf.TokenSource(ctx, &oauth2.Token{
RefreshToken: auth.OauthToken.RefreshToken,
}).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)
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
}

func RefreshGmailClient(ctx context.Context, auth TriggerAuth) (*http.Client, error) {
// Manually recreate the oauthtoken
conf := &oauth2.Config{
Expand Down Expand Up @@ -3866,4 +3914,4 @@ func VerifyIdToken(ctx context.Context, idToken string) (IdTokenCheck, error) {
}

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