charonc

package
v0.13.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 17, 2017 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewActorContext

func NewActorContext(ctx context.Context, a Actor) context.Context

NewActorContext returns a new Context that carries Actor value.

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.

func ActorFromContext

func ActorFromContext(ctx context.Context) (Actor, bool)

ActorFromContext returns the Actor value stored in context, if any.

type Client

type Client struct {
	// 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 Charon instance with given options.

func (*Client) Actor

func (c *Client) Actor(ctx context.Context, token string) (*Actor, error)

Actor implements Charon interface.

func (*Client) FromContext

func (c *Client) FromContext(ctx context.Context) (*Actor, error)

FromContext implements Charon interface.

func (*Client) IsAuthenticated

func (c *Client) IsAuthenticated(ctx context.Context, token string) (bool, error)

IsAuthenticated implements Charon interface.

func (*Client) IsGranted

func (c *Client) IsGranted(ctx context.Context, userID int64, perm charon.Permission) (bool, error)

IsGranted implements Charon interface.

func (*Client) Login

func (c *Client) Login(ctx context.Context, username, password string) (string, error)

Login is a simple wrapper around rpc Login method.

func (*Client) Logout

func (c *Client) Logout(ctx context.Context, token string) error

Logout implements Charon interface.

type Options

type Options func(*charonOptions)

Options configures how we set up the Client.

func WithMetadata

func WithMetadata(kv ...string) Options

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL