🐶 goal 的用户认证(Auth)模块完成,离生产环境又双叒叕近了一步!

2022-02-03 23:58:51 +08:00
 qbhy

Goal-web/auth

goal-web/auth
goal 框架的用户认证组件,你可以在 httpwebsocket 或者其他环境使用,有上下文即可。

安装 - install

go get github.com/goal-web/auth

使用 - usage

goal 的脚手架自带了绝大多数开发一个 web 应用的所需要的功能和组件,当然这也包括了认证组件。一般情况下,我们只需要在 .env 修改自己的认证配置即可,比如 jwt 驱动的 secret 、session 驱动的 session_key 。

配置 - config

默认情况下,config/auth.go 配置文件像下面那样,默认添加了 jwtsession 两个守卫配置

package config

import (
	"github.com/goal-web/auth"
	"github.com/goal-web/contracts"
	"github.com/goal-web/example/models"
	"github.com/golang-jwt/jwt"
)

func init() {
	configs["auth"] = func(env contracts.Env) interface{} {
		return auth.Config{
			Defaults: struct {
				Guard string
				User  string
			}{
				Guard: env.StringOption("auth.default", "jwt"), // 默认守卫
				User:  env.StringOption("auth.user", "db"), // 默认用户提供者
			},
			Guards: map[string]contracts.Fields{
				"jwt": { // 守卫名称
					"driver":   "jwt", // 驱动,目前支持 jwt 、session
					"secret":   env.GetString("auth.jwt.secret"), // jwt 签名所需的 secret ,不同的守卫建议不同的 secret
					"method":   jwt.SigningMethodHS256, // jwt 签名方法
					"lifetime": 60 * 60 * 24, // token 有效时长,单位:秒
					"provider": "db", // 用户提供者名
				},
				"session": { // 守卫名称
					"driver":      "session", // 驱动名
					"provider":    "db", // 用户提供者名
					// session 驱动所需的参数,如果应用需要配置多个 session 驱动的守卫,那么需要配置不一样的 session_key
					"session_key": env.StringOption("auth.session.key", "auth_session"), 
				},
			},
			Users: map[string]contracts.Fields{ // 用户提供者,目前只支持 db
				"db": { // 用户提供者名称
					"driver": "db", // 驱动名称
					"model":  models.UserModel, // 用户模型
				},
			},
		}
	}
}

.env 的数据库相关配置

# 默认连接
auth.jwt.secret=jwt_secret
auth.default=jwt

定义模型 - define a model

app/models/user.go 文件

package models

import (
	"github.com/goal-web/database/table"
	"github.com/goal-web/supports/class"
)

var (
	UserModel = table.NewModel(class.Make(new(User)), "users")
)

func UserQuery() *table.Table {
	return table.FromModel(UserModel)
}

type User struct {
	Id       string `json:"id"`
	NickName string `json:"name"`
}

// GetId 实现了 contracts.Authenticatable 接口,此方法必不可少
func (u User) GetId() string {
	return u.Id
}

用法 - method of use

package controllers

import (
	"github.com/goal-web/contracts"
	"github.com/goal-web/example/models"
)

func LoginExample(guard contracts.Guard) contracts.Fields {
	//  这是伪代码
	user := models.UserQuery().First().(models.User)

	return contracts.Fields{
		"token": guard.Login(user), // jwt 返回 token ,session 返回 true
	}
}

func GetCurrentUser(guard contracts.Guard) interface{} {
	return contracts.Fields{
		"user": guard.User(), // 已登录返回用户模型,否则返回 nil
	}
}

使用中间件

package routes

import (
	"github.com/goal-web/auth"
	"github.com/goal-web/contracts"
	"github.com/goal-web/example/app/http/controllers"
	"github.com/goal-web/session"
)

func ApiRoutes(router contracts.Router) {
    
    v1 := router.Group("", session.StartSession)
    
    // 直接应用在路由上
    v1.Get("/myself", controllers.GetCurrentUser, auth.Guard("jwt"))
    
    // 应用在路由组上
    authRouter := v1.Group("", auth.Guard("jwt"))
    authRouter.Get("/myself", controllers.GetCurrentUser, auth.Guard("jwt"))

}

守卫 API - guard api

type Guard interface {
	Once(user Authenticatable)
	User() Authenticatable
	GetId() string
	Check() bool
	Guest() bool
	Login(user Authenticatable) interface{}
}

扩展守卫和用户提供者 - extension

这部分内容比较多,这里暂时不展开讲,后面会专门录视频介绍,欢迎大家点赞订阅

在 goal 之外的框架使用 - use in frameworks other than goal

这部分内容比较多,这里暂时不展开讲,后面会专门录视频介绍,欢迎大家点赞订阅

goal-web
goal-web/auth
qbhy0715@qq.com

1952 次点击
所在节点    Go 编程语言
5 条回复
tousfun
2022-02-06 23:23:24 +08:00
给楼主点赞
qbhy
2022-02-08 22:38:00 +08:00
@919615766 谢谢。我给自己顶一个
qbhy
2022-02-08 22:39:51 +08:00
截止到现在我发这条回复为止,goal 的队列组件、限流、序列化等组件都已经完成了,队列组件目前支持 卡夫卡和 nsq ,并且都支持延迟队列,后续还会继续完善和优化,欢迎大家关注。
xqcode
2022-03-08 14:37:16 +08:00
顶一个
qbhy
2022-03-08 14:57:27 +08:00
@xqcode 谢谢老哥

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/831820

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX