()
| 54 | } |
| 55 | |
| 56 | func AuthMySQL() gin.HandlerFunc { |
| 57 | return func(c *gin.Context) { |
| 58 | var ( |
| 59 | tokenString string |
| 60 | token *jwt.Token |
| 61 | err error |
| 62 | link string |
| 63 | ) |
| 64 | tokenString, _ = c.Cookie("token") |
| 65 | if token, err = ValidateToken(tokenString); err != nil { |
| 66 | c.SetCookie("token", "", -1, "/", "", false, true) |
| 67 | c.AbortWithStatusJSON(http.StatusOK, gin.H{"code": http.StatusUnauthorized, "link": link, "message": err.Error()}) |
| 68 | return |
| 69 | } |
| 70 | claims, ok := token.Claims.(jwt.MapClaims) |
| 71 | if !ok { |
| 72 | c.AbortWithStatus(http.StatusInternalServerError) |
| 73 | return |
| 74 | } |
| 75 | if username, ok := claims["email"]; ok { |
| 76 | if user := mydb.getUserProfile(username.(string)); user == nil { |
| 77 | c.AbortWithStatusJSON(http.StatusOK, gin.H{"code": http.StatusUnauthorized, "message": "user not found"}) |
| 78 | c.SetCookie("token", "", -1, "/", "", false, true) |
| 79 | return |
| 80 | } |
| 81 | c.Set("username", username) |
| 82 | } |
| 83 | c.Next() |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | func JudgePermission(actionName string, username string, appID string, apiKey string) bool { |
| 88 | url := fmt.Sprintf("%s/permission?action=%s&username=%s", strings.TrimRight(config.IamURL, "/"), actionName, username) |
no test coverage detected