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: make the outlook adapter try to get a new access token with the refresh token #108

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
85 changes: 76 additions & 9 deletions internal/adapter/outlook_http/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package outlook_http

import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"

Expand Down Expand Up @@ -84,21 +87,44 @@ func (c *CalendarAPI) SetupOauth2(credentials auth.Credentials, storage auth.Sto
return err
}

// this only checks the expiry field, which is the expiration time of the access token which was granted
// even if the refresh token is still valid
// TODO: unfortunately, without this part - the token will get assigned below and this triggers a panic
// TODO: in the oauth2 package. I'm not aware of the culprit yet.
now := time.Now()
// If the access token is expired
if now.After(expiry) {
c.logger.Info("saved credentials expired, we need to reauthenticate..")
c.authenticated = false
err := c.storage.RemoveCalendarAuth(c.calendarID)

// trying to get a new access and refresh token using the refresh token
c.logger.Info("stored access token expired, lets try to get a new one..")
newAuthData, err := getNewRefreshToken(storedAuth.OAuth2.RefreshToken, credentials.Client.Id, endpoint.TokenURL)
if err != nil {
return fmt.Errorf("failed to remove authentication for calendar %s: %w", c.calendarID, err)
c.logger.Info("couldn't get a new access and refresh token, maybe the refresh token is also expired..")
c.authenticated = false
err = c.storage.RemoveCalendarAuth(c.calendarID)
if err != nil {
return fmt.Errorf("failed to remove authentication for calendar %s: %w", c.calendarID, err)
}
return nil
}

// we got a new access token, yay! 🎉
newAuthExpiry, err := time.Parse(time.RFC3339, newAuthData.Expiry)
if err != nil {
return err
}

c.oAuthToken = &oauth2.Token{
AccessToken: newAuthData.AccessToken,
RefreshToken: newAuthData.RefreshToken,
Expiry: newAuthExpiry,
TokenType: newAuthData.TokenType,
}
c.authenticated = true

c.logger.Debugf("Got a new access token, which expires on: %s", c.oAuthToken.Expiry.Format(time.RFC3339))
c.logger.Info("Refreshed credentials")

return nil
}

// if the access token isn't expired, we can use our stored Credentials
c.oAuthToken = &oauth2.Token{
AccessToken: storedAuth.OAuth2.AccessToken,
RefreshToken: storedAuth.OAuth2.RefreshToken,
Expand All @@ -107,7 +133,7 @@ func (c *CalendarAPI) SetupOauth2(credentials auth.Credentials, storage auth.Sto
}

c.authenticated = true
c.logger.Info("using stored credentials")
c.logger.Info("stored credentials are still valid, we will use those")
}

return nil
Expand Down Expand Up @@ -223,3 +249,44 @@ func (c *CalendarAPI) Name() string {
func (c *CalendarAPI) SetLogger(logger *log.Logger) {
c.logger = logger
}

// getNewRefreshToken gets a new access and refresh token from microsoft in exchange for a still valid refresh token and the clientID
func getNewRefreshToken(currentRefreshToken string, clientID string, tokenUrl string) (a auth.OAuth2Object, e error) {
data := url.Values{}
data.Set("grant_type", "refresh_token")
data.Set("refresh_token", currentRefreshToken)
data.Set("client_id", clientID)

req, err := http.NewRequest("POST", tokenUrl, strings.NewReader(data.Encode()))
if err != nil {
return a, err
}

req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

client := &http.Client{}

resp, err := client.Do(req)
if err != nil {
return a, err
}

respBody, err := io.ReadAll(resp.Body)
if err != nil {
return a, err
}

var tokenResponse TokenResponse

err = json.Unmarshal(respBody, &tokenResponse)
if err != nil {
return a, err
}

a.AccessToken = tokenResponse.AccessToken
a.RefreshToken = tokenResponse.RefreshToken
a.TokenType = tokenResponse.TokenType
a.Expiry = time.Now().Add(time.Duration(time.Second * time.Duration(tokenResponse.ExpiresIn))).Format(time.RFC3339)

return a, nil
}
9 changes: 9 additions & 0 deletions internal/adapter/outlook_http/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,12 @@ type EmailAddress struct {
type Location struct {
Name string `json:"displayName"`
}

type TokenResponse struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
Scope string `json:"scope"`
RefreshToken string `json:"refresh_token"`
IDToken string `json:"id_token"`
}