user

package
v0.0.0-...-d3da680 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2024 License: Apache-2.0, Apache-2.0 Imports: 2 Imported by: 7

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddFilters

func AddFilters(c context.Context, filts ...Filter) context.Context

AddFilters adds RawInterface filters to the context.

func IsAdmin

func IsAdmin(c context.Context) bool

IsAdmin returns true if the current user is an administrator for this AppEngine project.

func LoginURL

func LoginURL(c context.Context, dest string) (string, error)

LoginURL returns a URL that, when visited, prompts the user to sign in, then redirects the user to the URL specified by dest.

func LoginURLFederated

func LoginURLFederated(c context.Context, dest, identity string) (string, error)

LoginURLFederated is like LoginURL but accepts a user's OpenID identifier.

func LogoutURL

func LogoutURL(c context.Context, dest string) (string, error)

LogoutURL returns a URL that, when visited, signs the user out, then redirects the user to the URL specified by dest.

func OAuthConsumerKey

func OAuthConsumerKey(c context.Context) (string, error)

OAuthConsumerKey returns the OAuth consumer key provided with the current request.

This method will return an error if the OAuth request was invalid.

func Set

Set sets the user service in this context. Useful for testing with a quick mock. This is just a shorthand SetFactory invocation to set a factory which always returns the same object.

func SetFactory

func SetFactory(c context.Context, f Factory) context.Context

SetFactory sets the function to produce user.RawInterface instances, as returned by the Get method.

Types

type Factory

type Factory func(context.Context) RawInterface

Factory is the function signature for factory methods compatible with SetFactory.

type Filter

Filter is the function signature for a filter user implementation. It gets the current user implementation, and returns a new user implementation backed by the one passed in.

type RawInterface

type RawInterface interface {
	Current() *User
	CurrentOAuth(scopes ...string) (*User, error)

	IsAdmin() bool

	LoginURL(dest string) (string, error)
	LoginURLFederated(dest, identity string) (string, error)
	LogoutURL(dest string) (string, error)

	OAuthConsumerKey() (string, error)

	// If this implementation supports it, this will return an instance of the
	// Testable object for this service, which will let you 'log in' virtual users
	// in your test cases. If the implementation doesn't support it, it will
	// return nil.
	GetTestable() Testable
}

RawInterface provides access to the "appengine/users" API methods.

func Raw

Raw pulls the user service implementation from context or nil if it wasn't set.

type Testable

type Testable interface {
	// SetUser sets the user to a pre-populated User object.
	SetUser(*User)

	// Login will generate and set a new User object with values derived from
	// email clientID, and admin values. If clientID is provided, the User will
	// look like they logged in with OAuth. If it's empty, then this will look
	// like they logged in via the cookie auth method.
	Login(email, clientID string, admin bool)

	// Equivalent to SetUser(nil), but a bit more obvious to read in the code :).
	Logout()
}

Testable is the interface that test implimentations will provide.

func GetTestable

func GetTestable(c context.Context) Testable

GetTestable returns a Testable for the current task queue service in c, or nil if it does not offer one.

The Testable instance will let you 'log in' virtual users in your test cases.

type User

type User struct {
	Email             string
	AuthDomain        string
	Admin             bool
	ID                string
	ClientID          string
	FederatedIdentity string
	FederatedProvider string
}

User is a mimic of https://godoc.org/google.golang.org/appengine/user#User

It's provided here for convenience, and is compile-time checked to be identical.

func Current

func Current(c context.Context) *User

Current returns the currently logged-in user, or nil if the user is not signed in.

func CurrentOAuth

func CurrentOAuth(c context.Context, scopes ...string) (*User, error)

CurrentOAuth returns the user associated with the OAuth consumer making this request.

If the OAuth consumer did not make a valid OAuth request, or the scopes is non-empty and the current user does not have at least one of the scopes, this method will return an error.

Jump to

Keyboard shortcuts

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