charonc

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2018 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, act 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 {

	// 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) Actor

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

Actor returns Actor for given token if logged in.

func (*Client) FromContext

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

FromContext works like Actor but retrieves access token from the context.

func (*Client) IsAuthenticated

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

IsAuthenticated returns true if given access token exists.

func (*Client) IsGranted

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

IsGranted returns true if user has granted given permission.

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 removes given token making actor logged out.

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