swap out context access for account (#1589)
This commit is contained in:
parent
9bb1904a38
commit
f908110fb6
35
auth/auth.go
35
auth/auth.go
@ -3,11 +3,8 @@ package auth
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/micro/go-micro/v2/metadata"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -90,8 +87,6 @@ type Token struct {
|
|||||||
const (
|
const (
|
||||||
// DefaultNamespace used for auth
|
// DefaultNamespace used for auth
|
||||||
DefaultNamespace = "go.micro"
|
DefaultNamespace = "go.micro"
|
||||||
// MetadataKey is the key used when storing the account in metadata
|
|
||||||
MetadataKey = "auth-account"
|
|
||||||
// TokenCookieName is the name of the cookie which stores the auth token
|
// TokenCookieName is the name of the cookie which stores the auth token
|
||||||
TokenCookieName = "micro-token"
|
TokenCookieName = "micro-token"
|
||||||
// SecretCookieName is the name of the cookie which stores the auth secret
|
// SecretCookieName is the name of the cookie which stores the auth secret
|
||||||
@ -100,34 +95,18 @@ const (
|
|||||||
BearerScheme = "Bearer "
|
BearerScheme = "Bearer "
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type accountKey struct{}
|
||||||
|
|
||||||
// AccountFromContext gets the account from the context, which
|
// AccountFromContext gets the account from the context, which
|
||||||
// is set by the auth wrapper at the start of a call. If the account
|
// is set by the auth wrapper at the start of a call. If the account
|
||||||
// is not set, a nil account will be returned. The error is only returned
|
// is not set, a nil account will be returned. The error is only returned
|
||||||
// when there was a problem retrieving an account
|
// when there was a problem retrieving an account
|
||||||
func AccountFromContext(ctx context.Context) (*Account, error) {
|
func AccountFromContext(ctx context.Context) (*Account, bool) {
|
||||||
str, ok := metadata.Get(ctx, MetadataKey)
|
acc, ok := ctx.Value(accountKey{}).(*Account)
|
||||||
// there was no account set
|
return acc, ok
|
||||||
if !ok {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var acc *Account
|
|
||||||
// metadata is stored as a string, so unmarshal to an account
|
|
||||||
if err := json.Unmarshal([]byte(str), &acc); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return acc, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ContextWithAccount sets the account in the context
|
// ContextWithAccount sets the account in the context
|
||||||
func ContextWithAccount(ctx context.Context, account *Account) (context.Context, error) {
|
func ContextWithAccount(ctx context.Context, account *Account) context.Context {
|
||||||
// metadata is stored as a string, so marshal to bytes
|
return context.WithValue(ctx, accountKey{}, account)
|
||||||
bytes, err := json.Marshal(account)
|
|
||||||
if err != nil {
|
|
||||||
return ctx, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// generate a new context with the MetadataKey set
|
|
||||||
return metadata.Set(ctx, MetadataKey, string(bytes)), nil
|
|
||||||
}
|
}
|
||||||
|
@ -178,10 +178,7 @@ func AuthHandler(fn func() auth.Auth) server.HandlerWrapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// There is an account, set it in the context
|
// There is an account, set it in the context
|
||||||
ctx, err = auth.ContextWithAccount(ctx, account)
|
ctx = auth.ContextWithAccount(ctx, account)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// The user is authorised, allow the call
|
// The user is authorised, allow the call
|
||||||
return h(ctx, req, rsp)
|
return h(ctx, req, rsp)
|
||||||
|
Loading…
Reference in New Issue
Block a user