oauth

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2022 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package oauth 提供基于 OAuth2 的登录和注册功能

## 大致流程

  1. 前端访问 authURL
  2. 三方返回前端的 callback 页面
  3. callback 提交 {username: 'vendor id', password: 'code'}
  4. 后端的登录页调用 Authenticator.Valid 验证验录,如果未注册则自动注册;
  5. 登录页返回 token 给 callback 页,由该页面决定如何处理;

Index

Constants

View Source
const (
	TwitterAuthURL  = "https://api.twitter.com/2/oauth2/authorize"
	TwitterTokenURL = "https://api.twitter.com/2/oauth2/token"
)

Variables

View Source
var TwitterScopes = []string{"tweet.read", "users.read"}

Functions

func Install

func Install(mod string, db *orm.DB)

Types

type GetUserInfoFunc

type GetUserInfoFunc[T UserInfo] func(*oauth2.Token) (T, error)

GetUserInfoFunc 获取用户信息的方法

OAuth 并未规定返回的用户信息字段,该方法只能由用户根据平台自行实现。

type OAuth

type OAuth[T UserInfo] struct {
	// contains filtered or unexported fields
}

OAuth 表示 oauth2 登录的验证器

func New

func New[T UserInfo](s *web.Server, prefix orm.Prefix, db *orm.DB, c *oauth2.Config, g GetUserInfoFunc[T]) *OAuth[T]

New 声明 OAuth 对象

r 表示注册用户的方法,如果为 nil,那么碰到为未注册的用户时,OAuth.Valid 直接返回 false, 否则的话将尝试注册了新用户并返回新用户的 id。 prefix 表名前缀,当有多个不同实例时,prefix 不能相同。

func (*OAuth[T]) AuthURL

func (o *OAuth[T]) AuthURL() string

AuthURL 返回验证地址

func (*OAuth[T]) Delete

func (o *OAuth[T]) Delete(uid int64) error

func (*OAuth[T]) Identity

func (o *OAuth[T]) Identity(uid int64) (string, bool)

func (*OAuth[T]) Valid

func (o *OAuth[T]) Valid(state, code string) (int64, string, bool)

Valid 验证登录信息

code 为 oauth 服务第一步返回的 code 值;

type TwitterUserInfo

type TwitterUserInfo struct {
	ID   string `json:"id"`
	Name string `json:"name"`
}

func TwitterGetUserInfo

func TwitterGetUserInfo(token *oauth2.Token) (*TwitterUserInfo, error)

func (*TwitterUserInfo) Identity

func (info *TwitterUserInfo) Identity() string

type UserInfo

type UserInfo interface {
	// Identity 返回表示在服务器表示用户唯一 ID 的字符串
	Identity() string
}

UserInfo 表示 OAuth 登录后获取的用户信息

Jump to

Keyboard shortcuts

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