oauth2

package module
v2.1.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2016 License: Apache-2.0 Imports: 1 Imported by: 0

README

基于Golang的OAuth2服务实现

完全模块化、支持http/fasthttp的服务端处理、令牌存储支持redis/mongodb

GoDoc Go Report Card

获取

$ go get -u gopkg.in/oauth2.v2/...

HTTP服务端

package main

import (
	"log"
	"net/http"

	"gopkg.in/oauth2.v2/manage"
	"gopkg.in/oauth2.v2/models"
	"gopkg.in/oauth2.v2/server"
	"gopkg.in/oauth2.v2/store/client"
	"gopkg.in/oauth2.v2/store/token"
)

func main() {
	manager := manage.NewRedisManager(
		&token.RedisConfig{Addr: "192.168.33.70:6379"},
	)
	manager.MapClientStorage(client.NewTempStore())
	srv := server.NewServer(server.NewConfig(), manager)

	http.HandleFunc("/authorize", func(w http.ResponseWriter, r *http.Request) {
		authReq, err := srv.GetAuthorizeRequest(r)
		if err != nil {
			http.Error(w, err.Error(), http.StatusBadRequest)
			return
		}
		// TODO: 登录验证、授权处理
        authReq.UserID = "000000"

		err = srv.HandleAuthorizeRequest(w, authReq)
		if err != nil {
			http.Error(w, err.Error(), http.StatusBadRequest)
		}
	})

	http.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) {
		err := srv.HandleTokenRequest(w, r)
		if err != nil {
			http.Error(w, err.Error(), http.StatusBadRequest)
		}
	})

	log.Fatal(http.ListenAndServe(":9096", nil))
}

FastHTTP服务端

srv := server.NewFastServer(server.NewConfig(), manager)

fasthttp.ListenAndServe(":9096", func(ctx *fasthttp.RequestCtx) {
	switch string(ctx.Request.URI().Path()) {
	case "/authorize":
		authReq, err := srv.GetAuthorizeRequest(ctx)
		if err != nil {
			ctx.Error(err.Error(), 400)
			return
		}
		authReq.UserID = "000000"
		// TODO: 登录验证、授权处理
		err = srv.HandleAuthorizeRequest(ctx, authReq)
		if err != nil {
			ctx.Error(err.Error(), 400)
		}
	case "/token":
		err := srv.HandleTokenRequest(ctx)
		if err != nil {
			ctx.Error(err.Error(), 400)
		}
	}
})

测试

goconvey

$ goconvey -port=9092

范例

模拟授权码模式的测试范例,请查看example

License

Copyright 2016.All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AccessGenerate

type AccessGenerate interface {
	// 访问令牌、更新令牌
	Token(data *GenerateBasic, isGenRefresh bool) (access, refresh string, err error)
}

AccessGenerate 访问令牌生成接口

type AuthorizeGenerate

type AuthorizeGenerate interface {
	// 授权令牌
	Token(data *GenerateBasic) (code string, err error)
}

AuthorizeGenerate 授权令牌生成接口

type ClientInfo

type ClientInfo interface {
	// 客户端ID
	GetID() string
	// 客户端秘钥
	GetSecret() string
	// 客户端域名URL
	GetDomain() string
	// 扩展数据
	GetExtraData() interface{}
}

ClientInfo 客户端信息模型接口

type ClientStore

type ClientStore interface {
	// GetByID 根据ID获取客户端信息
	GetByID(id string) (ClientInfo, error)
}

ClientStore 客户端信息存储接口

type GenerateBasic

type GenerateBasic struct {
	Client   ClientInfo // 客户端信息
	UserID   string     // 用户标识
	CreateAt time.Time  // 创建时间
}

GenerateBasic 提供生成令牌的基础数据

type GrantType

type GrantType string

GrantType 定义授权模式

const (
	// AuthorizationCodeCredentials 授权码模式
	AuthorizationCodeCredentials GrantType = "authorization_code"
	// PasswordCredentials 密码模式
	PasswordCredentials GrantType = "password"
	// ClientCredentials 客户端模式
	ClientCredentials GrantType = "clientcredentials"
	// RefreshCredentials 更新令牌模式
	RefreshCredentials GrantType = "refreshtoken"
)

func (GrantType) String

func (gt GrantType) String() string

type Manager

type Manager interface {
	// GetClient 获取客户端信息
	// clientID 客户端标识
	GetClient(clientID string) (cli ClientInfo, err error)

	// GenerateAuthToken 生成授权令牌
	// rt 授权类型
	// tgr 生成令牌的请求参数
	GenerateAuthToken(rt ResponseType, tgr *TokenGenerateRequest) (authToken TokenInfo, err error)

	// GenerateAccessToken 生成访问令牌、更新令牌
	// rt 授权模式
	// tgr 生成令牌的请求参数
	GenerateAccessToken(rt GrantType, tgr *TokenGenerateRequest) (accessToken TokenInfo, err error)

	// RefreshAccessToken 更新访问令牌
	// tgr 生成令牌的请求参数
	RefreshAccessToken(tgr *TokenGenerateRequest) (accessToken TokenInfo, err error)

	// RemoveAccessToken 删除访问令牌
	// access 访问令牌
	RemoveAccessToken(access string) (err error)

	// RemoveRefreshToken 删除更新令牌
	// refresh 更新令牌
	RemoveRefreshToken(refresh string) (err error)

	// LoadAccessToken 加载访问令牌信息
	// access 访问令牌
	LoadAccessToken(access string) (ti TokenInfo, err error)

	// LoadRefreshToken 加载更新令牌信息
	// refresh 更新令牌
	LoadRefreshToken(refresh string) (ti TokenInfo, err error)
}

Manager OAuth2授权管理接口

type ResponseType

type ResponseType string

ResponseType 定义授权类型

const (
	// Code 授权码类型
	Code ResponseType = "code"
	// Token 令牌类型
	Token ResponseType = "token"
)

func (ResponseType) String

func (rt ResponseType) String() string

type TokenGenerateRequest

type TokenGenerateRequest struct {
	ClientID          string // 客户端标识
	ClientSecret      string // 客户端密钥
	UserID            string // 用户标识
	RedirectURI       string // 重定向URI
	Scope             string // 授权范围
	Code              string // 授权码(授权码模式使用)
	Refresh           string // 刷新令牌
	IsGenerateRefresh bool   // 是否生成更新令牌
}

TokenGenerateRequest 提供生成令牌的请求参数

type TokenInfo

type TokenInfo interface {
	// 客户端ID
	GetClientID() string
	// 设置客户端ID
	SetClientID(string)
	// 用户ID
	GetUserID() string
	// 设置用户ID
	SetUserID(string)
	// 重定向URI
	GetRedirectURI() string
	// 设置重定向URI
	SetRedirectURI(string)
	// 权限范围
	GetScope() string
	// 设置权限范围
	SetScope(string)
	// 令牌授权类型
	GetAuthType() string
	// 设置令牌授权类型
	SetAuthType(string)

	// 访问令牌(或授权令牌)
	GetAccess() string
	// 设置访问令牌(或授权令牌)
	SetAccess(string)
	// 访问令牌(或授权令牌)创建时间
	GetAccessCreateAt() time.Time
	// 设置访问令牌(或授权令牌)创建时间
	SetAccessCreateAt(time.Time)
	// 访问令牌(或授权令牌)有效期
	GetAccessExpiresIn() time.Duration
	// 设置访问令牌(或授权令牌)有效期
	SetAccessExpiresIn(time.Duration)

	// 更新令牌
	GetRefresh() string
	// 设置更新令牌
	SetRefresh(string)
	// 更新令牌创建时间
	GetRefreshCreateAt() time.Time
	// 设置更新令牌创建时间
	SetRefreshCreateAt(time.Time)
	// 更新令牌有效期
	GetRefreshExpiresIn() time.Duration
	// 设置更新令牌有效期
	SetRefreshExpiresIn(time.Duration)
}

TokenInfo 令牌信息模型接口

type TokenStore

type TokenStore interface {
	// Create 创建并存储新的令牌信息
	Create(info TokenInfo) error

	// RemoveByAccess 使用访问令牌删除令牌信息
	RemoveByAccess(access string) error

	// RemoveByRefresh 使用更新令牌删除令牌信息
	RemoveByRefresh(refresh string) error

	// 使用访问令牌获取令牌信息数据
	GetByAccess(access string) (TokenInfo, error)

	// 根据更新令牌获取令牌信息数据
	GetByRefresh(refresh string) (TokenInfo, error)
}

TokenStore 令牌信息存储接口

Directories

Path Synopsis
example
store

Jump to

Keyboard shortcuts

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