auth

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2024 License: MIT Imports: 15 Imported by: 0

README

go-auth

介绍

三方登录OAuth: Google、Facebook、Vkontakte

验证器ATPAuth: Google Authenticator

软件架构

Gin MVC架构介绍 https://gitee.com/jishulangcom/go-gin-mvc

Config配置说明 https://gitee.com/jishulangcom/go-config

安装教程
  1. 克隆Git源码

    git clone git@gitee.com:jishulangcom/go-auth.git
    
  2. 更新依赖

    go mod tidy
    
使用说明
swag docs使用
# 安装 swag
go install github.com/swaggo/swag/cmd/swag@latest

# 生成文档
运行main.go里的 //go:generate swag init --parseDependency --parseDepth=6 --instanceName api -o ./docs

# 访问
运行main.go,看命令窗口输出的Swagger run at:
Oauth三登录

演示:http://127.0.0.1:10001/

settings
{
  "Port": 10001,
  "OauthConf": {
    "vkontakte": {
      "ClientId": "51965716",
      "ClientSecret": "TxGJHmoSIGRURXmTUkTd"
    },
    "facebook": {
      "ClientId": "2053615698411446",
      "ClientSecret": "74f27a47936359e8de5486bd00f41744"
    },
    "google": {
      "ClientId": "37922059578-0a7mkom4ipfrr3gmicufekukmma8afpq.apps.googleusercontent.com",
      "ClientSecret": "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"
    },
    "tiktok": {
      "ClientId": "aw5h1vza26u04i6t",
      "ClientSecret": "VQ0kJrHlzaoLzFPhciwlVsC1097clKyd"
    }
  }
}
三方平台设置
Authorized redirect URIs:http://127.0.0.1:10001/oauth
获取授权登录URL
# POST方式
http://127.0.0.1:10001/api/v1/oauth/url
请求:{
        "platform":"google",
        "redirect_uri":"http://127.0.0.1:10001/oauth",
        "state":"test"
    }

# GET方式
http://localhost:10001/api/v1/oauth/url?platform=google&state=google&redirect_uri=http://127.0.0.1:10001/oauth


返回:{
        "code": "",
        "data": {
            "url": "https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=37922059578-0a7mkom4ipfrr3gmicufekukmma8afpq.apps.googleusercontent.com&redirect_uri=http%3A%2F%2F127.0.0.1%3A10001%2Foauth&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email&state=test"
        },
        "message": ""
    }
    
# 错误码(失败时code返回错误码,message返回具体错误信息)
PARAMETER_ERROR        # 请求参数有误
UNKNOWN_ERROR          # 未知错误
CONFIG_WRONG           # 配置有误
获取授权登录账户信息
# POST方式
http://127.0.0.1:10001/api/v1/oauth/info
{
    "platform":"google",
    "redirect_uri":"http://127.0.0.1:10001/oauth",
    "code_type":"ACCESS_TOKEN",
    "code":"xxxxxxx"
}

# GET方式
http://localhost:10001/api/v1/oauth/info?platform=google&code=xxxxxxx&redirect_uri=http://127.0.0.1:10001/oauth

# 返回
{
	"code": "",
	"message": "",
	"data": {
		"id": "110091465563921571012",
		"email": "buckb1291@gmail.com",
		"name": "buck buck",
		"avatar": "https://lh3.googleusercontent.com/a/ACg8ocI2wwuGa4FCLEk26Oml2RuJpsFYuUxGNd0C7YwjOjnUT0cy0A=s96-c"
	}
}

# 错误码(失败时code返回错误码,message返回具体错误信息)
PARAMETER_ERROR        # 请求参数有误
UNKNOWN_ERROR          # 未知错误
CONFIG_WRONG           # 配置有误
INVALID_CODE           # 无效的CODE(过期或code格式不对)
ATPAuth 验证器

Documentation

Index

Constants

View Source
const (
	ERR_CODE_PARAMETER_ERROR     = "PARAMETER_ERROR"     // 参数错误
	ERR_CODE_FAIL                = "FAIL"                // 失败
	ERR_CODE_UNKNOWN_ERROR       = "UNKNOWN_ERROR"       // 未知错误
	ERR_CODE_RECORD_NOT_FOUND    = "RECORD_NOT_FOUND"    // 未找到记录
	ERR_CODE_FREQUENT_OPERATIONS = "FREQUENT_OPERATIONS" // 频繁操作
	ERR_CODE_TYPE_INVALID_CODE   = "INVALID_CODE"        // 无效的CODE(过期或code格式不对)
	ERR_CODE_ACCESS_TOKEN_FAIL   = "ACCESS_TOKEN_FAIL"   // 获取ACCESS_TOKEN失败
	ERR_CODE_OAUTH_CONFIG_FAIL   = "OAUTH_CONFIG_FAIL"   // 获取OAUTH配置失败
)
View Source
const (
	CODE_TYPE_GRANT_CODE   = "grant_code"   // APP使用
	CODE_TYPE_ACCESS_TOKEN = "access_token" // WEB使用
)

code type

View Source
const GOOGLE_AUTHENTICATOR_EXPIRE_SECOND = 30

Variables

This section is empty.

Functions

func Request

func Request(method string, url string, data interface{}) ([]byte, error)

Types

type GoogleAuth

type GoogleAuth struct {
}

func (*GoogleAuth) GetCode

func (this *GoogleAuth) GetCode(secret string, expireSecond int64) (string, error)

获取动态码

func (*GoogleAuth) GetQrcode

func (this *GoogleAuth) GetQrcode(issuer string, user string, secret string, digits int, expireSecond int) string

获取动态码二维码内容

func (*GoogleAuth) GetQrcodeUrl

func (this *GoogleAuth) GetQrcodeUrl(qrcode string, width int, height int) string

获取动态码二维码图片地址,这里是第三方二维码api

func (*GoogleAuth) GetSecret

func (this *GoogleAuth) GetSecret() string

获取秘钥

func (*GoogleAuth) VerifyCode

func (this *GoogleAuth) VerifyCode(secret, code string, expireSecond int64) (bool, error)

验证动态码

type GoogleAuthenticator

type GoogleAuthenticator struct {
}

func NewGoogleAuthenticator

func NewGoogleAuthenticator() *GoogleAuthenticator

func (*GoogleAuthenticator) InitAuth

func (p *GoogleAuthenticator) InitAuth(issuer string, user string, width int, height int) (secret, qrCodeUrl string)

开启二次认证

func (*GoogleAuthenticator) VerifyCode

func (p *GoogleAuthenticator) VerifyCode(secret string, code string) (ok bool, err error)

type OauthObj

type OauthObj struct {
	PlatformConf OauthPlatformDto
	PlatformObj  *oauth2.Config
}

func NewOauth

func NewOauth(platform, redirectURL, clientId, clientSecret string) *OauthObj

func (*OauthObj) GetAccessToken

func (p *OauthObj) GetAccessToken(code string) (*TokenInfoDto, string, string)

获取access_token

type OauthPlatformDto

type OauthPlatformDto struct {
	Platform string
	BaseUrl  string
	Version  string
	AuthURL  string
	TokenURL string
	Scopes   []string
}

func (*OauthPlatformDto) GetUserInfo

func (o *OauthPlatformDto) GetUserInfo(accessToken string) (*UserInfoDto, string, string)

type ReqDto

type ReqDto struct {
	Issuer   string `json:"-"`
	Username string `json:"username"`
	Width    int    `json:"-"`
	Height   int    `json:"-"`
}

获取

type ResDto

type ResDto struct {
	Secret    string `json:"secret"`
	QrCodeUrl string `json:"qr_code_url"`
}

type TokenInfoDto

type TokenInfoDto struct {
	AccessToken string `json:"access_token"`
	TokenType   string `json:"token_type"`
	ExpiresIn   int    `json:"expires_in"`
}

type UserInfoDto

type UserInfoDto struct {
	Id            string `json:"id"`
	Email         string `json:"email"`
	VerifiedEmail bool   `json:"verified_email"`
	Name          string `json:"name"`
	Picture       string `json:"picture"`
}

type VerifyReqDto

type VerifyReqDto struct {
	Secret string `json:"secret"`
	Code   string `json:"code"`
}

验证

type VerifyResDto

type VerifyResDto struct {
	VerifyResult bool `json:"verify_result"`
}

Jump to

Keyboard shortcuts

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