client

package
v0.8.7 Latest Latest
Warning

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

Go to latest
Published: May 5, 2019 License: MIT Imports: 16 Imported by: 0

README

Staffio client

Example

package main

import (
	"fmt"
	"net/http"

	staffio "github.com/liut/staffio/client"
)

func main() {

	loginPath := "/auth/login"
	staffio.SetLoginPath(loginPath)
	staffio.SetAdminPath("/admin")

	http.HandleFunc(loginPath, staffio.LoginHandler)
	http.Handle("/auth/callback", staffio.AuthCodeCallback("admin"))

	authF1 := staffio.AuthMiddleware(true) // auto redirect
	http.Handle("/admin", authF1(http.HandlerFunc(handlerAdminWelcome)))
	// more handlers
}

func handlerAdminWelcome(w http.ResponseWriter, r *http.Request) {
	user := staffio.UserFromContext(r.Context())
	fmt.Printf("user: %s", user.Name)
}

Documentation

Index

Constants

View Source
const (
	TokenKey ctxKey = iota
	UserKey
)
View Source
const (
	SessKeyUser  = "user"
	SessKeyToken = "token"
)

Variables

View Source
var (
	ErrNoToken = errors.New("oauth2 token not found")
	ErrNoRole  = errors.New("the user not in special roles")

	AdminPath = "/admin/"
	LoginPath = "/auth/login"

	UserFromRequest = auth.UserFromRequest
)
View Source
var (
	SessionIDCookieName = "_sess"
)

Functions

func AuthCodeCallback added in v0.8.1

func AuthCodeCallback(roleName ...string) http.Handler

AuthCodeCallback Handler for Check auth with role[s] when auth-code callback

func AuthCodeCallbackWrap

func AuthCodeCallbackWrap(next http.Handler) http.Handler

AuthCodeCallbackWrap is a middleware that injects a InfoToken with roles into the context of callback request

func AuthMiddleware added in v0.8.1

func AuthMiddleware(redirect bool) func(next http.Handler) http.Handler

AuthMiddleware

func GetAuthCodeURL

func GetAuthCodeURL(state string) string

func GetOAuth2Config

func GetOAuth2Config() *oauth2.Config

func LoginHandler

func LoginHandler(w http.ResponseWriter, r *http.Request)

func SessionLoad added in v0.8.1

func SessionLoad(r *http.Request) session.Session

func SessionSave added in v0.8.1

func SessionSave(sess session.Session, w http.ResponseWriter)

func SetAdminPath added in v0.8.1

func SetAdminPath(path string)

func SetLoginPath added in v0.8.1

func SetLoginPath(path string)

func Setup

func Setup(redirectURL, clientID, clientSecret string, scopes []string)

Setup oauth2 config

func SetupSessionStore added in v0.8.1

func SetupSessionStore(store session.Store)

func TokenFromContext

func TokenFromContext(ctx context.Context) *oauth2.Token

TokenFromContext returns a oauth2.Token from the given context if one is present. Returns nil if a oauth2.Token cannot be found.

func UidFromToken

func UidFromToken(tok *oauth2.Token) string

UidFromToken extract uid from oauth2.Token

Types

type InfoToken

type InfoToken struct {
	AccessToken  string    `json:"access_token"`
	TokenType    string    `json:"token_type,omitempty"`
	RefreshToken string    `json:"refresh_token,omitempty"`
	ExpiresIn    int64     `json:"expires_in,omitempty"`
	Expiry       time.Time `json:"expiry,omitempty"`
	Me           Staff     `json:"me,omitempty"`
	Roles        RoleMe    `json:"group,omitempty"`
}

func AuthRequestWithRole added in v0.8.1

func AuthRequestWithRole(r *http.Request, role ...string) (it *InfoToken, err error)

AuthRequestWithRole called in AuthCallback

func RequestInfoToken

func RequestInfoToken(tok *oauth2.Token, roles ...string) (*InfoToken, error)

func (*InfoToken) GetExpiry

func (tok *InfoToken) GetExpiry() time.Time

type RoleMe

type RoleMe map[string]interface{}

func (RoleMe) Has

func (r RoleMe) Has(name string) bool

type Staff

type Staff struct {
	UID            string        `json:"uid" form:"uid"`                     // 登录名
	CommonName     string        `json:"cn,omitempty" form:"cn"`             // 全名
	GivenName      string        `json:"gn,omitempty" form:"gn"`             // 名
	Surname        string        `json:"sn,omitempty" form:"sn"`             // 姓
	Nickname       string        `json:"nickname,omitempty" form:"nickname"` // 昵称
	Birthday       string        `json:"birthday,omitempty" form:"birthday"` // 生日
	Gender         common.Gender `json:"gender,omitempty"`                   // 1=male, 2=female, 0=unknown
	Mobile         string        `json:"mobile,omitempty"`                   // cell phone number
	Email          string        `json:"email,omitempty"`
	EmployeeNumber int           `json:"eid,omitempty" form:"eid"`
	EmployeeType   string        `json:"etype,omitempty" form:"etitle"`
	AvatarPath     string        `json:"avatarPath,omitempty" form:"avatar"`
	Provider       string        `json:"provider,omitempty"`
}

Staff is a retrieved employee struct.

type User added in v0.8.1

type User = auth.User

func UserFromContext added in v0.8.1

func UserFromContext(ctx context.Context) *User

UserFromContext

func UserFromSession added in v0.8.2

func UserFromSession(sess session.Session) (u *User, ok bool)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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