Documentation
¶
Index ¶
- func NewActorContext(ctx context.Context, act Actor) context.Context
- type Actor
- type Client
- func (c *Client) Actor(ctx context.Context, token string) (*Actor, error)
- func (c *Client) FromContext(ctx context.Context) (*Actor, error)
- func (c *Client) IsAuthenticated(ctx context.Context, token string) (bool, error)
- func (c *Client) IsGranted(ctx context.Context, userID int64, perm charon.Permission) (bool, error)
- func (c *Client) Login(ctx context.Context, username, password string) (string, error)
- func (c *Client) Logout(ctx context.Context, token string) error
- type Options
- type SecurityContext
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Actor ¶
type Actor struct {
ID int64 `json:"id"`
Username string `json:"username"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
IsSuperuser bool `json:"isSuperuser"`
IsActive bool `json:"isActive"`
IsStaff bool `json:"isStaff"`
IsConfirmed bool `json:"isConfirmed"`
Permissions charon.Permissions `json:"permissions"`
}
Actor is a generic object that represent anything that can be under control of charon.
type Client ¶
type Client struct {
// RPCClient holds gRPC client from charonrpc package.
// It's not safe to change concurrently.
RPCClient charonrpc.AuthClient
// contains filtered or unexported fields
}
Client is simplified version of rpc AuthClient. It contains most commonly used methods. For more powerful low level API check RPCClient interface.
func New ¶
func New(conn *grpc.ClientConn, options ...Options) *Client
New allocates new Client instance with given options and gRPC connection.
func (*Client) FromContext ¶
FromContext works like Actor but retrieves access token from the context.
func (*Client) IsAuthenticated ¶
IsAuthenticated returns true if given access token exists.
type Options ¶
type Options func(*charonOptions)
Options configures how we set up the Client.
func WithMetadata ¶
WithMetadata sets metadata that will be attachable to every request.
type SecurityContext ¶
type SecurityContext interface {
context.Context
oauth2.TokenSource
// Actor ...
Actor() (Actor, bool)
// AccessToken ...
AccessToken() (string, bool)
}
SecurityContext ....
Example ¶
token := "0000000001some hash"
subject := Actor{
ID: 1,
Username: "j.kowalski@gmail.com",
}
ctx := NewActorContext(context.Background(), subject)
ctx = mnemosyne.NewAccessTokenContext(ctx, token)
sctx := NewSecurityContext(ctx)
var (
t *oauth2.Token
act Actor
err error
ok bool
)
if t, err = sctx.Token(); err != nil {
fmt.Printf("unexpected error: %s", err.Error())
} else {
fmt.Println(t.AccessToken)
}
if act, ok = sctx.Actor(); ok {
fmt.Println(act.ID)
fmt.Println(act.Username)
}
Output: 0000000001some hash 1 j.kowalski@gmail.com
func NewSecurityContext ¶
func NewSecurityContext(ctx context.Context) SecurityContext
NewSecurityContext allocates new context.
Click to show internal directories.
Click to hide internal directories.