@@ -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 {
4240var _ port.Configurable = & ROCalendarAPI {}
4341var _ port.LogSetter = & ROCalendarAPI {}
4442var _ port.CalendarIDSetter = & ROCalendarAPI {}
43+ var _ port.StorageSetter = & ROCalendarAPI {}
4544
4645func (c * ROCalendarAPI ) SetCalendarID (calendarID string ) error {
4746 if calendarID == "" {
@@ -52,7 +51,15 @@ func (c *ROCalendarAPI) SetCalendarID(calendarID string) error {
5251}
5352
5453func (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 {
113131func (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+ }
0 commit comments