micro/util/auth/auth.go

81 lines
1.7 KiB
Go
Raw Normal View History

2020-05-14 16:10:14 +01:00
package auth
import (
"time"
"github.com/google/uuid"
"github.com/micro/go-micro/v3/auth"
"github.com/micro/go-micro/v3/logger"
2020-05-14 16:10:14 +01:00
)
// Verify the auth credentials and refresh the auth token periodically
func Verify(a auth.Auth) error {
2020-05-14 16:10:14 +01:00
// extract the account creds from options, these can be set by flags
accID := a.Options().ID
accSecret := a.Options().Secret
// if no credentials were provided, self generate an account
if len(accID) == 0 && len(accSecret) == 0 {
2020-05-14 16:10:14 +01:00
opts := []auth.GenerateOption{
auth.WithType("service"),
2020-05-21 14:56:17 +01:00
auth.WithScopes("service"),
2020-05-14 16:10:14 +01:00
}
acc, err := a.Generate(uuid.New().String(), opts...)
2020-05-14 16:10:14 +01:00
if err != nil {
return err
}
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
logger.Debugf("Auth [%v] Generated an auth account", a.String())
}
2020-05-14 16:10:14 +01:00
accID = acc.ID
accSecret = acc.Secret
}
// generate the first token
token, err := a.Token(
auth.WithCredentials(accID, accSecret),
auth.WithExpiry(time.Minute*10),
)
if err != nil {
return err
}
// set the credentials and token in auth options
a.Init(
auth.ClientToken(token),
auth.Credentials(accID, accSecret),
)
// periodically check to see if the token needs refreshing
go func() {
timer := time.NewTicker(time.Second * 15)
for {
<-timer.C
// don't refresh the token if it's not close to expiring
tok := a.Options().Token
if tok.Expiry.Unix() > time.Now().Add(time.Minute).Unix() {
continue
}
// generate the first token
tok, err := a.Token(
2020-05-20 11:59:01 +01:00
auth.WithToken(tok.RefreshToken),
2020-05-14 16:10:14 +01:00
auth.WithExpiry(time.Minute*10),
)
if err != nil {
logger.Warnf("[Auth] Error refreshing token: %v", err)
continue
}
// set the token
a.Init(auth.ClientToken(tok))
}
}()
return nil
}