<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
JSON Web Toekn(JWT)是一個開放標準RFC 7519,以JSON的方式進行通訊,是目前最流行的一種身份驗證方式之一。
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
下圖是通過JWT.io解碼,檢視JWT token的組成
可以看出JWT是由下面三個部分組成的:
Header是Token的構成的第一部分,包含了Token型別、Token使用的加密演演算法(加密演演算法可以是HMAC, SHA256或者是RSA等)。在某些場景下,可以使用kid欄位,用來標識一個金鑰的ID
Payload是token的第二部分,由JWT標準中註冊的、公共的、私有的宣告三部分組成。Payload通常包含一些使用者的宣告資訊,比如簽發者、過期時間、簽發時間等。其中最常見的是issuer, expiration, subject。
Signature是由頭部和載荷加密後連線起來的,程式通過驗證Signature是否合法來決定認證是否通過
需要注意的是不要將敏感資訊存在Token裡面!!!
Casbin是一個強大的、高效的、開源的許可權存取控制庫,它提供了多種許可權控制存取模型,比如ACL(許可權控制列表)、RBAC(基於角色的存取控制)、ABAC(基於屬性的許可權驗證)等。除此之外Casbin還支援多種程式語言
{subject, object, action}
或者自定義的模式執行想要的策略,同時支援allow和deny兩種授權方式在Casbin中,存取控制模型被抽象為PERM(Policy, Effect, Request, Matcher)
的一個檔案
定義請求引數。基本請求時一個元組物件,至少需要主題(存取實體), 物件(存取資源), 動作(存取方式),例如r={sub, obj, act}
,它實際定義了我們應該提供存取控制匹配功能的引數名稱和順序
定義存取策略模式,例如p={sub, obj, act}或p={sub, obj, act, eff}
, 它定義欄位的名稱和順序
匹配請求和策略的規則,例如m = r.sub == p.sub && r.obj == p.obj && r.act == p.act
,它的意思是如果請求的引數被匹配,那麼結果就會被返回
匹配後的結果會儲存於Effect當中,可以對匹配結果再次做出邏輯判斷,例如e = some (where (p.eft == allow))
Casbin中最基本的model是ACL,下面是ACL的model設定
[request_definition] r = sub, obj, act # Policy definition [policy_definition] p = sub, obj, act # Policy effect [policy_effect] e = some(where (p.eft == allow)) # Matchers [matchers] m = r.sub == p.sub && r.obj == p.obj && r.act == p.act
編寫一個簡單的TODO
RESTful API。
建立一個simple-jwt-auth
的目錄,然後通過go mod
管理依賴 go mod init simple-jwt-auth
建立的目錄結構如下:
在model定義User
和Todo
的結構體
// models/model.go type User struct { ID string `json:"id"` UserName string `json:"username"` Password string `json:"password"` } type Todo struct { UserID string `json:"user_id"` Title string `json:"title"` Body string `json:"body"` } // SetPassword sets a new password stored as hash. func (m *User) SetPassword(password string) error { if len(password) < 6 { return fmt.Errorf("new password for %s must be at least 6 characters", m.UserName) } m.Password = password return nil } // InvalidPassword returns true if the given password does not match the hash. func (m *User) InvalidPassword(password string) bool { if password == "" { return true } if m.Password != password { return true } return false }
當用戶通過使用者名稱和密碼等資訊登入系統服務時,需要驗證是否已註冊、密碼是否正確等,然後返回資訊, 下面在api
層實現Login
的介面:
// api/auth_api.go func Login(c *gin.Context) { var u models.User if err := c.ShouldBindJSON(&u); err != nil { c.JSON(http.StatusUnprocessableEntity, "Invalid json provided") return } //find user with username user, err := models.UserRepo.FindByID(1) //compare the user from the request, with the one we defined: if user.UserName != u.UserName || user.Password != u.Password { c.JSON(http.StatusUnauthorized, "Please provide valid login details") return } c.JSON(http.StatusOK, "Login successfully") } func Logout(c *gin.Context) { c.JSON(http.StatusOK, "Successfully logged out") }
在真實的專案中,資料都是存在資料庫中。在該教學中,為了方便,建立一個mock檔案user_repository.go
// models/user_repository.go var us = []User{ { ID: "2", UserName: "users", Password: "pass", }, { ID: "3", UserName: "username", Password: "password", }, } var UserRepo = UserRepository{ Users: us, } type UserRepository struct { Users []User } func (r *UserRepository) FindAll() ([]User, error) { return r.Users, nil } func (r *UserRepository) FindByID(id int) (User, error) { for _, v := range r.Users { uid, err := strconv.Atoi(v.ID) if err != nil { return User{}, err } if uid == int(id) { return v, nil } } return User{}, errors.New("Not found") } func (r *UserRepository) Save(user User) (User, error) { r.Users = append(r.Users, user) return user, nil } func (r *UserRepository) Delete(user User) { id := -1 for i, v := range r.Users { if v.ID == user.ID { id = i break } } if id == -1 { log.Fatal("Not found user ") return } r.Users[id] = r.Users[len(r.Users)-1] // Copy last element to index i. r.Users[len(r.Users)-1] = User{} // Erase last element (write zero value). r.Users = r.Users[:len(r.Users)-1] // Truncate slice. return }
為了不讓Login
函數變得臃腫,生成token
的邏輯放在auth
目錄中, 下面實現token
驗證邏輯
用JWT
實現的系統中,使用者登入後,系統會生成並返回一個token
給使用者,下次請求時將會帶上該token
進行身份驗證。token有以下問題需要處理:
token
有可能被駭客劫持和使用token
過期後需要使用者重新登入,體驗不友好上面的問題可以通過以下兩種方式解決:
使用uuid
作為redis中的key, token資訊作為value, 下面定義TokenManager
結構體,通過介面的方式實現token
type TokenManager struct{} func NewTokenService() *TokenManager { return &TokenManager{} } type TokenInterface interface { CreateToken(userId, userName string) (*TokenDetails, error) ExtractTokenMetadata(*http.Request) (*AccessDetails, error) } //Token implements the TokenInterface var _ TokenInterface = &TokenManager{} func (t *TokenManager) CreateToken(userId, userName string) (*TokenDetails, error) { td := &TokenDetails{} td.AtExpires = time.Now().Add(time.Minute * 30).Unix() //expires after 30 min td.TokenUuid = uuid.NewV4().String() td.RtExpires = time.Now().Add(time.Hour * 24 * 7).Unix() td.RefreshUuid = td.TokenUuid + "++" + userId var err error //Creating Access Token atClaims := jwt.MapClaims{} atClaims["access_uuid"] = td.TokenUuid atClaims["user_id"] = userId atClaims["user_name"] = userName atClaims["exp"] = td.AtExpires at := jwt.NewWithClaims(jwt.SigningMethodHS256, atClaims) td.AccessToken, err = at.SignedString([]byte(os.Getenv("ACCESS_SECRET"))) if err != nil { return nil, err } //Creating Refresh Token td.RtExpires = time.Now().Add(time.Hour * 24 * 7).Unix() td.RefreshUuid = td.TokenUuid + "++" + userId rtClaims := jwt.MapClaims{} rtClaims["refresh_uuid"] = td.RefreshUuid rtClaims["user_id"] = userId rtClaims["user_name"] = userName rtClaims["exp"] = td.RtExpires rt := jwt.NewWithClaims(jwt.SigningMethodHS256, rtClaims) td.RefreshToken, err = rt.SignedString([]byte(os.Getenv("REFRESH_SECRET"))) if err != nil { return nil, err } return td, nil } func (t *TokenManager) ExtractTokenMetadata(r *http.Request) (*AccessDetails, error) { token, err := VerifyToken(r) if err != nil { return nil, err } acc, err := Extract(token) if err != nil { return nil, err } return acc, nil } func TokenValid(r *http.Request) error { token, err := VerifyToken(r) if err != nil { return err } if _, ok := token.Claims.(jwt.Claims); !ok && !token.Valid { return err } return nil } func VerifyToken(r *http.Request) (*jwt.Token, error) { tokenString := ExtractToken(r) token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) { if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"]) } return []byte(os.Getenv("ACCESS_SECRET")), nil }) if err != nil { return nil, err } return token, nil } //get the token from the request body func ExtractToken(r *http.Request) string { bearToken := r.Header.Get("Authorization") strArr := strings.Split(bearToken, " ") if len(strArr) == 2 { return strArr[1] } return "" } func Extract(token *jwt.Token) (*AccessDetails, error) { claims, ok := token.Claims.(jwt.MapClaims) if ok && token.Valid { accessUuid, ok := claims["access_uuid"].(string) userId, userOk := claims["user_id"].(string) userName, userNameOk := claims["user_name"].(string) if ok == false || userOk == false || userNameOk == false { return nil, errors.New("unauthorized") } else { return &AccessDetails{ TokenUuid: accessUuid, UserId: userId, UserName: userName, }, nil } } return nil, errors.New("something went wrong") } func ExtractTokenMetadata(r *http.Request) (*AccessDetails, error) { token, err := VerifyToken(r) if err != nil { return nil, err } acc, err := Extract(token) if err != nil { return nil, err } return acc, nil }
上面的程式碼設定token的有效時間為30分鐘,30分鐘過後token將失效,使用者不能使用該token進行正確驗證。
另外,使用了從.env
組態檔獲取的金鑰(ACCESS_SECRET)簽名。在真實的專案,不能在程式碼中公開這個金鑰!!!
REDIS_HOST=127.0.0.1 REDIS_PORT=6379 REDIS_PASSWORD= ACCESS_SECRET=98hbun98hsdfsdwesdfs REFRESH_SECRET=786dfdbjhsbsdfsdfsdf PORT=8081
定義AuthInterface
處理對談
package auth import ( "errors" "fmt" "github.com/go-redis/redis/v7" "time" ) type AccessDetails struct { TokenUuid string UserId string UserName string } type TokenDetails struct { AccessToken string RefreshToken string TokenUuid string RefreshUuid string AtExpires int64 RtExpires int64 } type AuthInterface interface { CreateAuth(string, *TokenDetails) error FetchAuth(string) (string, error) DeleteRefresh(string) error DeleteTokens(*AccessDetails) error } type RedisAuthService struct { client *redis.Client } var _ AuthInterface = &RedisAuthService{} func NewAuthService(client *redis.Client) *RedisAuthService { return &RedisAuthService{client: client} } //Save token metadata to Redis func (tk *RedisAuthService) CreateAuth(userId string, td *TokenDetails) error { at := time.Unix(td.AtExpires, 0) //converting Unix to UTC(to Time object) rt := time.Unix(td.RtExpires, 0) now := time.Now() atCreated, err := tk.client.Set(td.TokenUuid, userId, at.Sub(now)).Result() if err != nil { return err } rtCreated, err := tk.client.Set(td.RefreshUuid, userId, rt.Sub(now)).Result() if err != nil { return err } if atCreated == "0" || rtCreated == "0" { return errors.New("no record inserted") } return nil } //Check the metadata saved func (tk *RedisAuthService) FetchAuth(tokenUuid string) (string, error) { userid, err := tk.client.Get(tokenUuid).Result() if err != nil { return "", err } return userid, nil } //Once a user row in the token table func (tk *RedisAuthService) DeleteTokens(authD *AccessDetails) error { //get the refresh uuid refreshUuid := fmt.Sprintf("%s++%s", authD.TokenUuid, authD.UserId) //delete access token deletedAt, err := tk.client.Del(authD.TokenUuid).Result() if err != nil { return err } //delete refresh token deletedRt, err := tk.client.Del(refreshUuid).Result() if err != nil { return err } //When the record is deleted, the return value is 1 if deletedAt != 1 || deletedRt != 1 { return errors.New("something went wrong") } return nil } func (tk *RedisAuthService) DeleteRefresh(refreshUuid string) error { //delete refresh token deleted, err := tk.client.Del(refreshUuid).Result() if err != nil || deleted == 0 { return err } return nil }
在Casbin中,一個許可權存取控制模型的組態檔是基於PERM(Policy, Effect, Role, Matcher)
的方式,因此當要修改或升級許可權的時候非常方便,只需要修改組態檔就行了。使用者可以自定義組態檔,例如定義RBAC
或者ACL
最基本的也是最簡單的模型是ACL
, 下面建立一個ACL
的模型組態檔
[request_definition] r = sub, obj, act [policy_definition] p = sub, obj, act [role_definition] g = _, _ [policy_effect] e = some(where (p.eft == allow)) [matchers] m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act
Casbin 的許可權是儲存在.csv
檔案中或者是SQL
資料庫中, 在該教學是通過csv
檔案的方式儲存
p, user, resource, read p, username, resource, read p, admin, resource, read p, admin, resource, write g, alice, admin g, bob, user
上面許可權的意思是:
首先,定義一個policies的中介軟體
import ( "fmt" "github.com/casbin/casbin" "github.com/casbin/casbin/persist" "github.com/gin-gonic/gin" "github.com/simple-jwt-auth/auth" "log" "net/http" ) func TokenAuthMiddleware() gin.HandlerFunc { return func(c *gin.Context) { err := auth.TokenValid(c.Request) if err != nil { c.JSON(http.StatusUnauthorized, "unauthorized") c.Abort() return } c.Next() } } // Authorize determines if current subject has been authorized to take an action on an object. func Authorize(obj string, act string, adapter persist.Adapter) gin.HandlerFunc { return func(c *gin.Context) { err := auth.TokenValid(c.Request) if err != nil { c.JSON(http.StatusUnauthorized, "user hasn't logged in yet") c.Abort() return } metadata, err := auth.ExtractTokenMetadata(c.Request) if err != nil { c.JSON(http.StatusUnauthorized, "unauthorized") return } // casbin enforces policy ok, err := enforce(metadata.UserName, obj, act, adapter) //ok, err := enforce(val.(string), obj, act, adapter) if err != nil { log.Println(err) c.AbortWithStatusJSON(500, "error occurred when authorizing user") return } if !ok { c.AbortWithStatusJSON(403, "forbidden") return } c.Next() } } func enforce(sub string, obj string, act string, adapter persist.Adapter) (bool, error) { enforcer := casbin.NewEnforcer("config/rbac_model.conf", adapter) err := enforcer.LoadPolicy() if err != nil { return false, fmt.Errorf("failed to load policy from DB: %w", err) } ok := enforcer.Enforce(sub, obj, act) return ok, nil }
然後,修改上面的Login
和Logout
介面, 增加身份驗證及授權資訊
func Login(c *gin.Context) { var u models.User if err := c.ShouldBindJSON(&u); err != nil { c.JSON(http.StatusUnprocessableEntity, "Invalid json provided") return } //find user with username user, err := models.UserRepo.FindByID(1) //compare the user from the request, with the one we defined: if user.UserName != u.UserName || user.Password != u.Password { c.JSON(http.StatusUnauthorized, "Please provide valid login details") return } ts, err := tokenManager.CreateToken(user.ID, user.UserName) if err != nil { c.JSON(http.StatusUnprocessableEntity, err.Error()) return } save token to redis saveErr := servers.HttpServer.RD.CreateAuth(user.ID, ts) if saveErr != nil { c.JSON(http.StatusUnprocessableEntity, saveErr.Error()) } tokens := map[string]string{ "access_token": ts.AccessToken, "refresh_token": ts.RefreshToken, } c.JSON(http.StatusOK, tokens) } func Logout(c *gin.Context) { //If metadata is passed and the tokens valid, delete them from the redis store metadata, _ := tokenManager.ExtractTokenMetadata(c.Request) if metadata != nil { deleteErr := servers.HttpServer.RD.DeleteTokens(metadata) if deleteErr != nil { c.JSON(http.StatusBadRequest, deleteErr.Error()) return } } c.JSON(http.StatusOK, "Successfully logged out") }
定義Todo
的結構體
type Todo struct { UserID string `json:"user_id"` Title string `json:"title"` Body string `json:"body"` }
建立Todo
的介面
package api import ( "github.com/gin-gonic/gin" "github.com/simple-jwt-auth/auth" "github.com/simple-jwt-auth/models" "net/http" ) func CreateTodo(c *gin.Context) { var td models.Todo if err := c.ShouldBindJSON(&td); err != nil { c.JSON(http.StatusUnprocessableEntity, "invalid json") return } metadata, err := auth.ExtractTokenMetadata(c.Request) if err != nil { c.JSON(http.StatusUnauthorized, "unauthorized") return } td.UserID = metadata.UserId //you can proceed to save the to a database c.JSON(http.StatusCreated, td) } func GetTodo(c *gin.Context) { metadata, err := auth.ExtractTokenMetadata(c.Request) if err != nil { c.JSON(http.StatusUnauthorized, "unauthorized") return } userId := metadata.UserId c.JSON(http.StatusOK, models.Todo{ UserID: userId, Title: "Return todo", Body: "Return todo for testing", }) }
註冊路由
func (s *Server) InitializeRoutes() { s.Router.POST("/login", api.Login) authorized := s.Router.Group("/") authorized.Use(gin.Logger()) authorized.Use(gin.Recovery()) authorized.Use(middleware.TokenAuthMiddleware()) { authorized.POST("/api/todo", middleware.Authorize("resource", "write", s.FileAdapter), api.CreateTodo) authorized.GET("/api/todo", middleware.Authorize("resource", "read", s.FileAdapter), api.GetTodo) authorized.POST("/logout", api.Logout) authorized.POST("/refresh", api.Refresh) } }
最後在main.go
檔案中呼叫server
層的Run方法即可執行
package main import ( "github.com/joho/godotenv" "github.com/simple-jwt-auth/servers" "log" ) func init() { if err := godotenv.Load(); err != nil { log.Print("No .env file found") } } func main() { servers.Run() log.Println("Server exiting") }
結果如下:
原作者倉庫地址github
以上就是Golang基於JWT與Casbin身份驗證授權範例詳解的詳細內容,更多關於Go JWT Casbin身份驗證授權的資料請關注it145.com其它相關文章!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45