@@ -78,16 +78,15 @@ func (s *ukVATService) Validate(vatNumber string, opts ValidatorOpts) error {
7878
7979// UKAccessToken contains access token information used to authenticate with the UK VAT API.
8080type UKAccessToken struct {
81- Token string `json:"access_token"`
82- SecondsUntilExpires int64 `json:"expires_in"`
83- ExpiresAt int64 `json:"expires_at"`
84- IsTest bool `json:"-"`
81+ Token string `json:"access_token"`
82+ SecondsUntilExpires int64 `json:"expires_in"`
83+ ExpiresAt time. Time `json:"expires_at"`
84+ IsTest bool `json:"-"`
8585}
8686
8787// IsExpired checks if the access token is expired.
8888func (t UKAccessToken ) IsExpired () bool {
89- // for safety, consider it expired 1 minute before the actual expiration
90- return t .ExpiresAt - int64 (60 ) < time .Now ().Unix ()
89+ return t .ExpiresAt .Before (time .Now ())
9190}
9291
9392// GenerateUKAccessToken generates an access token from given client credentials for use with the UK VAT API.
@@ -136,7 +135,8 @@ func GenerateUKAccessToken(opts ValidatorOpts) (*UKAccessToken, error) {
136135 if err := json .NewDecoder (resp .Body ).Decode (& token ); err != nil {
137136 return nil , ErrUnableToGenerateUKAccessToken {Err : err }
138137 }
139- token .ExpiresAt = token .SecondsUntilExpires + time .Now ().Unix ()
138+ // set the expiration time to 1 minute before the actual expiration for safety
139+ token .ExpiresAt = time .Now ().Add (time .Duration (token .SecondsUntilExpires - 60 ))
140140 if opts .IsUKTest {
141141 token .IsTest = true
142142 }
0 commit comments