identify

package module
v0.0.0-...-444091e Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2019 License: MIT Imports: 12 Imported by: 2

README

bulrush-identify

Provide basic user authorization and authentication

var Identify = identify.
	New().
	AddOptions(identify.FakeURLsOption([]string{`^/api/ignore$`, `^/api/gorm/mock`})).
	AddOptions(identify.FakeTokensOption([]string{})).
	AddOptions(identify.ModelOption(&identify.RedisModel{
		Redis: addition.Redis,
	})).
	Init(func(iden *identify.Identify) {
		iden.AddOptions(
			identify.AuthOption(func(ctx *gin.Context) (interface{}, error) {
				login := struct {
					UserName string `form:"username" json:"username" xml:"username" binding:"required"`
					Password string `form:"password" json:"password" xml:"password" binding:"required"`
					Type     string `form:"type" json:"type" xml:"type"`
				}{}
				// captcha := ctx.GetString("captcha")
				if err := ctx.ShouldBind(&login); err != nil {
					return nil, err
				}
				user := addition.GORMExt.Var("User")
				if err := addition.GORMExt.DB.Find(user, map[string]interface{}{"name": login.UserName, "password": login.Password}).Error; err != nil {
					return nil, errors.New("user authentication failed")
				}
				return user, nil
			}),
		)
	})
app.Use(Identify)
ObtainToken
double@double:~/bulrush-template$ curl http://127.0.0.1:3001/api/v1/obtainToken -X POST -d "username=123&password=xxx"
{
  "accessToken":"E8V3TZX3XQEWN6E8B0VJTXK26Z8OF6F5",
  "refreshToken":"U1MPQIYQZKD86CYGWKDI04O4R5SK4FEW",
  "created":1559287003,
  "updated":1559287003,
  "expired":1559373403
}
RefleshToken
double@double:~/bulrush-template$ curl http://127.0.0.1:3001/api/v1/refleshToken -X POST -d "accessToken=U1MPQIYQZKD86CYGWKDI04O4R5SK4FEW"
{
  "accessToken":"0ILMFDM0JILCLJ5987HYMT2J18BYJDXI",
  "refreshToken":"U1MPQIYQZKD86CYGWKDI04O4R5SK4FEW",
  "created":1559287003,
  "updated":1559287592,
  "expired":1559373992,
  "extra":{
    "id":"5ce8ba5e51603f528e568b47","username":"123","roles":[]
  }
}

MIT License

Copyright (c) 2018-2020 Double

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Index

Constants

View Source
const IdenKey = "identify"

IdenKey defined ctx key

Variables

This section is empty.

Functions

func RandString

func RandString(n int) string

RandString gen random string

func Some

func Some(target interface{}, initValue interface{}) interface{}

Some get or a default value

Types

type Identify

type Identify struct {
	Auth       func(c *gin.Context) (interface{}, error)
	Iden       func(c *gin.Context)
	ExpiresIn  int64
	Routes     RoutesGroup
	Model      Model
	FakeURLs   *[]string
	FakeTokens *[]string
}

Identify authentication interface

func New

func New() *Identify

New defined return a new struct

func (*Identify) AddOptions

func (iden *Identify) AddOptions(opts ...Option) *Identify

AddOptions defined add option

func (*Identify) GetToken

func (iden *Identify) GetToken(ctx *gin.Context) *Token

GetToken get from ctx

func (*Identify) Init

func (iden *Identify) Init(init func(*Identify)) *Identify

Init Identify

func (*Identify) ObtainToken

func (iden *Identify) ObtainToken(extra interface{}) (*Token, error)

ObtainToken accessToken

func (*Identify) Plugin

func (iden *Identify) Plugin(router *gin.RouterGroup, httpProxy *gin.Engine) *Identify

Plugin for bulrush

func (*Identify) SetToken

func (iden *Identify) SetToken(ctx *gin.Context, token *Token)

SetToken set to ctx

func (*Identify) VerifyContext

func (iden *Identify) VerifyContext(ctx *gin.Context) bool

VerifyContext defined verify

func (*Identify) VerifyToken

func (iden *Identify) VerifyToken(token string) bool

VerifyToken defined verify

type Model

type Model interface {
	Save(*Token) (*Token, error)
	Find(*Token) (*Token, error)
	Revoke(*Token) error
}

Model defined

type Option

type Option func(*Identify) interface{}

Option defined implement of option

func AuthOption

func AuthOption(auth func(ctx *gin.Context) (interface{}, error)) Option

AuthOption defined auth

func FakeTokensOption

func FakeTokensOption(f []string) Option

FakeTokensOption defined model

func FakeURLsOption

func FakeURLsOption(f []string) Option

FakeURLsOption defined model

func IdenOption

func IdenOption(iden func(c *gin.Context)) Option

IdenOption defined iden

func ModelOption

func ModelOption(model Model) Option

ModelOption defined model

type RedisModel

type RedisModel struct {
	Redis *redisext.Redis
}

RedisModel token store

func (*RedisModel) Find

func (model *RedisModel) Find(token *Token) (*Token, error)

Find find a token

func (*RedisModel) Revoke

func (model *RedisModel) Revoke(token *Token) error

Revoke revoke a token

func (*RedisModel) Save

func (model *RedisModel) Save(token *Token) (*Token, error)

Save save a token

type RoutesGroup

type RoutesGroup struct {
	ObtainTokenRoute  string
	RevokeTokenRoute  string
	RefleshTokenRoute string
	IdenTokenRoute    string
}

RoutesGroup iden routes

type Token

type Token struct {
	AccessToken  string
	RefreshToken string
	ExpiresIn    int64
	CreatedAt    int64
	UpdatedAt    int64
	Extra        interface{}
}

Token defined Token info

func (*Token) ExtraValue

func (t *Token) ExtraValue(key string) interface{}

ExtraValue defined Extra to spec type

func (*Token) ISValid

func (t *Token) ISValid() bool

ISValid defined token ISValid

func (*Token) Marshal

func (t *Token) Marshal() (string, error)

Marshal defined to json

func (*Token) MarshalExtra

func (t *Token) MarshalExtra(target interface{}) error

MarshalExtra defined Extra to spec type

func (*Token) Unmarshal

func (t *Token) Unmarshal(data string) error

Unmarshal defined from json

Jump to

Keyboard shortcuts

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