Refactor auth to load token outside wrappers
This commit is contained in:
@@ -1,90 +0,0 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
conf "github.com/micro/go-micro/v2/config"
|
||||
"github.com/micro/go-micro/v2/config/source/file"
|
||||
"github.com/micro/go-micro/v2/util/log"
|
||||
)
|
||||
|
||||
// FileName for global micro config
|
||||
const FileName = ".micro"
|
||||
|
||||
// config is a singleton which is required to ensure
|
||||
// each function call doesn't load the .micro file
|
||||
// from disk
|
||||
var config = newConfig()
|
||||
|
||||
// Get a value from the .micro file
|
||||
func Get(path ...string) (string, error) {
|
||||
tk := config.Get(path...).String("")
|
||||
return strings.TrimSpace(tk), nil
|
||||
}
|
||||
|
||||
// Set a value in the .micro file
|
||||
func Set(value string, path ...string) error {
|
||||
// get the filepath
|
||||
fp, err := filePath()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// set the value
|
||||
config.Set(value, path...)
|
||||
|
||||
// write to the file
|
||||
return ioutil.WriteFile(fp, config.Bytes(), 0644)
|
||||
}
|
||||
|
||||
func filePath() (string, error) {
|
||||
usr, err := user.Current()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return filepath.Join(usr.HomeDir, FileName), nil
|
||||
}
|
||||
|
||||
// newConfig returns a loaded config
|
||||
func newConfig() conf.Config {
|
||||
// get the filepath
|
||||
fp, err := filePath()
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return conf.DefaultConfig
|
||||
}
|
||||
|
||||
// write the file if it does not exist
|
||||
if _, err := os.Stat(fp); os.IsNotExist(err) {
|
||||
ioutil.WriteFile(fp, []byte{}, 0644)
|
||||
} else if err != nil {
|
||||
log.Error(err)
|
||||
return conf.DefaultConfig
|
||||
}
|
||||
|
||||
// create a new config
|
||||
c, err := conf.NewConfig(
|
||||
conf.WithSource(
|
||||
file.NewSource(
|
||||
file.WithPath(fp),
|
||||
),
|
||||
),
|
||||
)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return conf.DefaultConfig
|
||||
}
|
||||
|
||||
// load the config
|
||||
if err := c.Load(); err != nil {
|
||||
log.Error(err)
|
||||
return conf.DefaultConfig
|
||||
}
|
||||
|
||||
// return the conf
|
||||
return c
|
||||
}
|
@@ -12,7 +12,6 @@ import (
|
||||
"github.com/micro/go-micro/v2/errors"
|
||||
"github.com/micro/go-micro/v2/metadata"
|
||||
"github.com/micro/go-micro/v2/server"
|
||||
"github.com/micro/go-micro/v2/util/config"
|
||||
)
|
||||
|
||||
type fromServiceWrapper struct {
|
||||
@@ -159,50 +158,13 @@ func (a *authWrapper) Call(ctx context.Context, req client.Request, rsp interfac
|
||||
return a.Client.Call(ctx, req, rsp, opts...)
|
||||
}
|
||||
|
||||
// performs the call with the authorization token provided
|
||||
callWithToken := func(token string) error {
|
||||
ctx := metadata.Set(ctx, "Authorization", auth.BearerScheme+token)
|
||||
return a.Client.Call(ctx, req, rsp, opts...)
|
||||
}
|
||||
|
||||
// check to see if we have a valid access token
|
||||
aaOpts := aa.Options()
|
||||
if aaOpts.Token != nil && aaOpts.Token.Expiry.Unix() > time.Now().Unix() {
|
||||
return callWithToken(aaOpts.Token.AccessToken)
|
||||
}
|
||||
|
||||
// check to ensure we're not calling auth, since this will result in
|
||||
// an endless loop
|
||||
if req.Service() == "go.micro.auth" {
|
||||
ctx = metadata.Set(ctx, "Authorization", auth.BearerScheme+aaOpts.Token.AccessToken)
|
||||
return a.Client.Call(ctx, req, rsp, opts...)
|
||||
}
|
||||
|
||||
// if we have a refresh token we can use this to generate another access token
|
||||
if aaOpts.Token != nil {
|
||||
tok, err := aa.Token(auth.WithToken(aaOpts.Token.RefreshToken))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
aa.Init(auth.ClientToken(tok))
|
||||
return callWithToken(tok.AccessToken)
|
||||
}
|
||||
|
||||
// generate a new token if we have credentials
|
||||
if len(aaOpts.ID) > 0 && len(aaOpts.Secret) > 0 {
|
||||
tok, err := aa.Token(auth.WithCredentials(aaOpts.ID, aaOpts.Secret))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
aa.Init(auth.ClientToken(tok))
|
||||
return callWithToken(tok.AccessToken)
|
||||
}
|
||||
|
||||
// check to see if a token was provided in config, this is normally used for
|
||||
// setting the token when calling via the cli
|
||||
if token, err := config.Get("micro", "auth", "token"); err == nil && len(token) > 0 {
|
||||
return callWithToken(token)
|
||||
}
|
||||
|
||||
// call without an auth token
|
||||
return a.Client.Call(ctx, req, rsp, opts...)
|
||||
}
|
||||
|
Reference in New Issue
Block a user