Skip to content

Commit ab17ef6

Browse files
committed
feat(outlook_token): store access token
1 parent 20044f2 commit ab17ef6

File tree

4 files changed

+47
-9
lines changed

4 files changed

+47
-9
lines changed

internal/adapter/outlook_token/adapter.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ type ROCalendarAPI struct {
3131
outlookClient ROOutlookCalendarClient
3232
calendarID string
3333

34-
accessToken string
35-
3634
logger *log.Logger
3735

3836
storage auth.Storage
@@ -42,6 +40,7 @@ type ROCalendarAPI struct {
4240
var _ port.Configurable = &ROCalendarAPI{}
4341
var _ port.LogSetter = &ROCalendarAPI{}
4442
var _ port.CalendarIDSetter = &ROCalendarAPI{}
43+
var _ port.StorageSetter = &ROCalendarAPI{}
4544

4645
func (c *ROCalendarAPI) SetCalendarID(calendarID string) error {
4746
if calendarID == "" {
@@ -52,7 +51,15 @@ func (c *ROCalendarAPI) SetCalendarID(calendarID string) error {
5251
}
5352

5453
func (c *ROCalendarAPI) Initialize(ctx context.Context, openBrowser bool, config map[string]interface{}) error {
55-
if c.accessToken == "" {
54+
storedAuth, err := c.storage.ReadCalendarAuth(c.calendarID)
55+
if err != nil {
56+
return err
57+
}
58+
accessToken := ""
59+
if storedAuth != nil && storedAuth.AccessToken.Expiry.After(time.Now()) {
60+
c.logger.Debug("adapter is already authenticated, loading access token")
61+
accessToken = storedAuth.AccessToken.AccessToken
62+
} else {
5663
if openBrowser {
5764
c.logger.Infof("opening browser window for authentication of %s\n", c.Name())
5865
err := browser.OpenURL(graphUrl)
@@ -81,13 +88,24 @@ func (c *ROCalendarAPI) Initialize(ctx context.Context, openBrowser bool, config
8188
return errors.New("Access token expired")
8289
}
8390

84-
c.accessToken = tokenString
85-
} else {
86-
c.logger.Debug("adapter is already authenticated, loading access token")
91+
c.logger.Infof("access token valid until %v", expirationTime.Time.Format(time.RFC1123))
92+
93+
accessToken = tokenString
94+
_, err = c.storage.WriteCalendarAuth(auth.CalendarAuth{
95+
CalendarID: c.calendarID,
96+
AccessToken: auth.AccessTokenObject{
97+
AccessToken: accessToken,
98+
Expiry: expirationTime.Time,
99+
},
100+
})
101+
if err != nil {
102+
return err
103+
}
104+
c.logger.Debugf("access token stored successfully")
87105
}
88106

89107
client := &http.Client{}
90-
c.outlookClient = &ROOutlookClient{Client: client, AccessToken: c.accessToken, CalendarID: c.calendarID}
108+
c.outlookClient = &ROOutlookClient{Client: client, AccessToken: accessToken, CalendarID: c.calendarID}
91109
return nil
92110
}
93111

@@ -113,3 +131,7 @@ func (c *ROCalendarAPI) Name() string {
113131
func (c *ROCalendarAPI) SetLogger(logger *log.Logger) {
114132
c.logger = logger
115133
}
134+
135+
func (c *ROCalendarAPI) SetStorage(storage auth.Storage) {
136+
c.storage = storage
137+
}

internal/adapter/port/interface.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ type CalendarIDSetter interface {
1818
SetCalendarID(calendarID string) error
1919
}
2020

21+
// StorageSetter can be implemented by a struct to use persistent storage
22+
type StorageSetter interface {
23+
SetStorage(storage auth.Storage)
24+
}
25+
2126
// Configurable is an interface which defines how arbitrary configuration data can be passed
2227
// to a struct which implements this interface. Clients should be configurable.
2328
type Configurable interface {

internal/adapter/source_adapter.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ func NewSourceAdapterFromConfig(ctx context.Context, bindPort uint, openBrowser
5858
}
5959
}
6060

61+
if c, ok := client.(port.StorageSetter); ok {
62+
c.SetStorage(storage)
63+
}
64+
6165
if c, ok := client.(port.OAuth2Adapter); ok {
6266
if err := c.SetupOauth2(ctx,
6367
auth.Credentials{

internal/auth/storage.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package auth
33
import (
44
"context"
55
"fmt"
6+
"time"
67

78
"github.com/inovex/CalendarSync/internal/config"
89
)
@@ -15,8 +16,9 @@ type Storage interface {
1516
}
1617

1718
type CalendarAuth struct {
18-
CalendarID string
19-
OAuth2 OAuth2Object
19+
CalendarID string
20+
OAuth2 OAuth2Object
21+
AccessToken AccessTokenObject
2022
}
2123

2224
type OAuth2Object struct {
@@ -26,6 +28,11 @@ type OAuth2Object struct {
2628
TokenType string
2729
}
2830

31+
type AccessTokenObject struct {
32+
AccessToken string
33+
Expiry time.Time
34+
}
35+
2936
func StorageFactory(typ string) (Storage, error) {
3037
switch typ {
3138
case "yaml":

0 commit comments

Comments
 (0)