oauth2server

package module
v0.0.0-...-b46a351 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2018 License: MIT Imports: 20 Imported by: 0

README

Auth2接口文档

#环境相关:

###说明: config.toml可以从URL的docker路径下下载

第一步修改config.toml配置文件:

[database] name = "mysql" username = "root" password = "123456" addr = "172.17.0.3" port = "3306" schema = "oauth2" local = "Asia/Shanghai" param = "?"

[redis] #user=root #password=123456 port="6379" addr="172.17.0.2" #db=8

第二步初始化数据库:

docker run -v [config.toml所在目录]:/home/config godcong/oauth2server /go/src/github.com/godcong/oauth2server/cmd -c "/home/config/config.toml" init

第三部运行服务:

docker run -it --name o2s -P -v [config.toml所在目录]:/home/config godcong/oauth2server

创建客户端:

docker run -v [config.toml所在目录]:/home/config godcong/oauth2server /go/src/github.com/godcong/oauth2server/cmd -c "/home/config/config.toml" client [redirect url]

服务相关:

Step 1. 登陆授权

跳转到:/authorize

参数:
  必填 response_type : "code"   //此处填写code  
    必填 client_id        //玛娜花园的授权客户端id  
    选填 state            //状态码  
    选填 redirect_uri     //回调地址,必须与服务端预留地址一致  
返回值:

code //用于获取token state //返回请求的state值

说明:

客户端跳转到授权界面获取登陆授权。授权成功服务器跳转至客户端注册的redirect url地址,并附带参数code,state

Step 2. 获取token

地址:/token

协议:POST
参数:
  必填 client_id        //玛娜花园的授权客户端id  
    必填 client_secret    //玛娜花园预留安全码
    必填 grant_type       //此处填authorization_code
    必填 code             //step1返回的code
    必填 redirect_uri     //回调地址,必须与服务端预留地址一致  
返回值:

access_token //用户访问授权码,通过该授权码获取内容 token_type //返回bearer refresh_token //访问授权码过期时,通过该授权码刷新访问授权码 expires_in //过期时间

Step 3. 获取信息

地址:/userinfo

协议:GET
参数:
  必填 access_token //用户访问授权码,通过该授权码获取内容
返回值:
"sub"               //用户唯一识别ID,同openid         
  "nickname":         //用户昵称
  "name":             //用户名
  "phone_number":     //手机号
  "email":            //邮箱

Documentation

Index

Constants

View Source
const (
	E_INVALID_REQUEST           = iota //"invalid_request"
	E_UNAUTHORIZED_CLIENT              //"unauthorized_client"
	E_ACCESS_DENIED                    //"access_denied"
	E_UNSUPPORTED_RESPONSE_TYPE        //"unsupported_response_type"
	E_INVALID_SCOPE                    //"invalid_scope"
	E_SERVER_ERROR                     //"server_error"
	E_TEMPORARILY_UNAVAILABLE          //"temporarily_unavailable"
	E_UNSUPPORTED_GRANT_TYPE           //"unsupported_grant_type"
	E_INVALID_GRANT                    //"invalid_grant"
	E_INVALID_CLIENT                   //"invalid_client"
	/*----------------------------------------------------*/
	E_INVALID_NONE //no errors
)
View Source
const DefaultClientName = "玛娜花园"
View Source
const HOST = "http://localhost:7788"
View Source
const TOKEN_PREFIX = "TK_"

Variables

View Source
var (
	//登录
	A_LOGIN_SUCCESS                = ResponseMessage{strconv.Itoa(0), "登陆成功!", nil}
	A_NAME_OR_PASSWORD_CANNOT_NULL = ResponseMessage{strconv.Itoa(1), "用户名或密码不能为空!", nil}
	A_MOBILE_OR_CODE_CANNOT_NULL   = ResponseMessage{strconv.Itoa(2), "手机或验证码不能为空!", nil}
	A_NAME_OR_PASSWORD_WRONG       = ResponseMessage{strconv.Itoa(3), "用户名或密码错误!", nil}
	A_COMPLETE_USERINFO_SUCCESS    = ResponseMessage{strconv.Itoa(4), "成功完善用户信息!!!", nil}

	//注册
	A_REGISTER_SUCCESS = ResponseMessage{strconv.Itoa(0), "注册成功!", nil}

	A_MUST_NOT_BE_NULL       = ResponseMessage{strconv.Itoa(1), "必填字段不能为空!", nil}
	A_NAME_FORMAT_WRONG      = ResponseMessage{strconv.Itoa(2), "用户名格式错误!", nil}
	A_NAME_IS_EXISTS         = ResponseMessage{strconv.Itoa(3), "用户名已经存在!", nil}
	A_NAME_LENGTH_WRONG      = ResponseMessage{strconv.Itoa(4), "用户名不能少于六位!", nil}
	A_MOBILE_LENGTH_WRONG    = ResponseMessage{strconv.Itoa(5), "手机号填写错误!", nil}
	A_MOBILE_IS_EXISTS       = ResponseMessage{strconv.Itoa(3), "手机号已经被注册!", nil}
	A_MESSAGE_WRONG          = ResponseMessage{strconv.Itoa(6), "手机号验证码错误!", nil}
	A_PASSWORD_LENGTH_WRONG  = ResponseMessage{strconv.Itoa(7), "密码不能少于六位!", nil}
	A_PASSWORD_COMPARE_WRONG = ResponseMessage{strconv.Itoa(8), "两次密码输入不一致!", nil}

	//
	A_MESSAGE_SEND_SUCCESS  = ResponseMessage{strconv.Itoa(0), "信息发送成功!", nil}
	A_MESSAGE_SEND_FAILED   = ResponseMessage{strconv.Itoa(1), "信息发送失败!", nil}
	A_MESSAGE_CHECK_SUCCESS = ResponseMessage{strconv.Itoa(0), "验证码正确!", nil}
	A_MESSAGE_CHECK_FAILED  = ResponseMessage{strconv.Itoa(1), "验证码错误!", nil}

	A_FORGET_CHECK_SUCCESS        = ResponseMessage{strconv.Itoa(0), "验证通过!", nil}
	A_FORGET_CHANGE_SUCCESS       = ResponseMessage{strconv.Itoa(0), "密码变更成功!", nil}
	A_FORGET_NEED_IS_NULL         = ResponseMessage{strconv.Itoa(1), "缺少必填字段!!", nil}
	A_FORGET_ACCOUNT_WRONG        = ResponseMessage{strconv.Itoa(2), "账户填写错误!!", nil}
	A_FORGET_PASSWORD_CANNOT_NULL = ResponseMessage{strconv.Itoa(3), "密码不能为空!", nil}
	A_FORGET_PASSWORD_FILL_OUT    = ResponseMessage{strconv.Itoa(4), "输入密码不一致!", nil}
	A_FORGET_SYSTEM_ERROR         = ResponseMessage{strconv.Itoa(5), "系统错误,请重试!", nil}

	//修改密码
	A_CHANGE_SUCCESS            = ResponseMessage{strconv.Itoa(0), "密码修改成功!", nil}
	A_CHANGE_NEED_IS_NULL       = ResponseMessage{strconv.Itoa(1), "缺少必填字段!!", nil}
	A_CHANGE_OPASSWORD_WRONG    = ResponseMessage{strconv.Itoa(2), "原密码错误!", nil}
	A_CHANGE_NPASSWORD_FILL_OUT = ResponseMessage{strconv.Itoa(3), "新输入密码不一致!", nil}
)
View Source
var (
	AuthorizationCodeGrant   = "authorization_code"
	PasswordCredentialsGrant = "password"
	ClientCredentialsGrant   = "client_credentials"
	RefreshToken             = "refresh_token"
)
View Source
var ERROR_MAP = map[int]error{
	E_INVALID_REQUEST:           errors.New("The request is missing a required parameter, includes an invalid parameter value, includes a parameter more than once, or is otherwise malformed."),
	E_UNAUTHORIZED_CLIENT:       errors.New("The client is not authorized to request a token using this method."),
	E_ACCESS_DENIED:             errors.New("The resource owner or authorization server denied the request."),
	E_UNSUPPORTED_RESPONSE_TYPE: errors.New("The authorization server does not support obtaining a token using this method."),
	E_INVALID_SCOPE:             errors.New("The requested scope is invalid, unknown, or malformed."),
	E_SERVER_ERROR:              errors.New("The authorization server encountered an unexpected condition that prevented it from fulfilling the request."),
	E_TEMPORARILY_UNAVAILABLE:   errors.New("The authorization server is currently unable to handle the request due to a temporary overloading or maintenance of the server."),
	E_UNSUPPORTED_GRANT_TYPE:    errors.New("The authorization grant type is not supported by the authorization server."),
	E_INVALID_GRANT:             errors.New("The provided authorization grant (e.g., authorization code, resource owner credentials) or refresh token is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client."),
	E_INVALID_CLIENT:            errors.New("Client authentication failed (e.g., unknown client, no client authentication included, or unsupported authentication method)."),
	E_INVALID_NONE:              nil,
}

Functions

func AccessCheck

func AccessCheck(c *gin.Context) error

func AccessGenerateToken

func AccessGenerateToken(cid, uid string, nano int64, genRefresh bool) (access, refresh string)

func AuthorizeGenerateToken

func AuthorizeGenerateToken(cid, uid string) (code string)

func CheckClientValidator

func CheckClientValidator(form url.Values) int

func CheckScope

func CheckScope(s, s1 string) []string

func CreateIfNotExist

func CreateIfNotExist(author *model.Authorize) (bool, *model.Authorize)

func DefaultDB

func DefaultDB() *gorm.DB

func DefaultRedisStrore

func DefaultRedisStrore() *sessions.RedisStore

func DefaultRouter

func DefaultRouter() *gin.Engine

func FinishAuthorize

func FinishAuthorize(c *gin.Context, u interface{}) (code string, e error)

func GetAccessTokenAndRefreshToken

func GetAccessTokenAndRefreshToken(client *model.Client, user *model.User) (acc, ref string)

validate: client_id Required. The client application's id. client_secret Required. The client application's client secret . grant_type Required. Must be set to authorization_code . code Required. The authorization code received by the authorization server. redirect_uri Required, if the request URI was included in the authorization request. Must be identical then.

func GetError

func GetError(code int) error

func GetInfo

func GetInfo(scope []string, user *model.User, author *model.Authorize) map[string]string

func GetRedis

func GetRedis() redis.Conn

func GetRefreshToken

func GetRefreshToken(c *gin.Context)

token过期,使用refresh_token重新获取

func GetTitleFromClient

func GetTitleFromClient(client *model.Client) string

func Home

func Home(e *gin.Engine) *gin.RouterGroup

func NewRedis

func NewRedis() *redisx.ConnMux

func NewRedisStore

func NewRedisStore() *sessions.RedisStore

func ParseClient

func ParseClient(form string) *model.Client

func ParseClientUser

func ParseClientUser(c *gin.Context) string

func ParseURL

func ParseURL(c *gin.Context, path string) *url.URL

func PasswordToken

func PasswordToken(c *gin.Context)

func PullForm

func PullForm(s *Session) string

func RawUrl

func RawUrl(s string, form url.Values) *url.URL

func RefreshCheck

func RefreshCheck()

func ResponseError

func ResponseError(c *gin.Context, code int)

func SignCheck

func SignCheck(c *gin.Context)

func Start

func Start()

func Token

func Token(c *gin.Context)

获取Token

func ValidatePhone

func ValidatePhone(c *gin.Context, s *Session) bool

func ValidateUser

func ValidateUser(c *gin.Context, s *Session) bool

return: continue flag

func VisitLog

func VisitLog(c *gin.Context)

Types

type ErrorResponse

type ErrorResponse struct {
	Error            string
	ErrorDescription string
	ErrorUri         string
	State            string
}

AuthorizationErrorResponse

type Oauth2Server

type Oauth2Server struct {
	// contains filtered or unexported fields
}

func DefaultOauth2Server

func DefaultOauth2Server() *Oauth2Server

func (*Oauth2Server) GetDB

func (gs *Oauth2Server) GetDB() *gorm.DB

func (*Oauth2Server) GetRedisStrore

func (gs *Oauth2Server) GetRedisStrore() *sessions.RedisStore

func (*Oauth2Server) GetRouter

func (gs *Oauth2Server) GetRouter() *gin.Engine

func (*Oauth2Server) Router

func (gs *Oauth2Server) Router()

func (*Oauth2Server) Run

func (gs *Oauth2Server) Run(addr string)

type ResponseMessage

type ResponseMessage struct {
	Code    string
	Message string
	Data    interface{}
}

type Session

type Session struct {
	// contains filtered or unexported fields
}

func AutoDefaultSession

func AutoDefaultSession(c *gin.Context) *Session

func (*Session) Delete

func (s *Session) Delete(key string) error

func (*Session) Get

func (s *Session) Get(key string) string

func (*Session) Set

func (s *Session) Set(key, val string) error

type UserAuthorizeInfo

type UserAuthorizeInfo struct {
	User model.User
}

Directories

Path Synopsis
main
client command
cmd command
server command

Jump to

Keyboard shortcuts

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