idk but it did something
This commit is contained in:
parent
95a94bfed5
commit
fbd23fe2cf
2 changed files with 36 additions and 20 deletions
|
@ -30,10 +30,10 @@ func New(redirectUri, clientId, clientSecret string) Client {
|
|||
}
|
||||
|
||||
func (c Client) GetUser(t auth.TokenResponse) (auth.User, error) {
|
||||
user := auth.User {
|
||||
Blacklisted: false,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
user := auth.User{
|
||||
Blacklisted: false,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/%s", api, "users/@me"), nil)
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package auth
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
@ -20,30 +21,45 @@ func GenerateJWT(key string, user User, expiryTimestamp uint64) (string, error)
|
|||
}
|
||||
|
||||
func invalidAuth(c *gin.Context) {
|
||||
c.String(http.StatusUnauthorized, "Unauthorized.")
|
||||
c.Abort()
|
||||
c.String(http.StatusUnauthorized, "Unauthorized.")
|
||||
c.Abort()
|
||||
}
|
||||
|
||||
func JwtMiddleware(secret string) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
jwtSplit := strings.Split(c.GetHeader("Authorization"), " ")
|
||||
return func(c *gin.Context) {
|
||||
jwtSplit := strings.Split(c.GetHeader("Authorization"), " ")
|
||||
|
||||
if jwtSplit[0] != "Bearer" {
|
||||
invalidAuth(c)
|
||||
return
|
||||
}
|
||||
if len(jwtSplit) < 2 || jwtSplit[0] != "Bearer" {
|
||||
invalidAuth(c)
|
||||
return
|
||||
}
|
||||
|
||||
claims, err := ValidateJWT(jwtSplit[1], secret)
|
||||
claims, err := ValidateJWT(jwtSplit[1], secret)
|
||||
if err != nil {
|
||||
invalidAuth(c)
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
invalidAuth(c)
|
||||
return
|
||||
}
|
||||
if userClaims, ok := claims["user"].(map[string]interface{}); ok {
|
||||
userJSON, err := json.Marshal(userClaims) // Convert map to JSON
|
||||
if err != nil {
|
||||
invalidAuth(c)
|
||||
return
|
||||
}
|
||||
|
||||
c.Set("claims", claims)
|
||||
var user User
|
||||
err = json.Unmarshal(userJSON, &user)
|
||||
if err != nil {
|
||||
invalidAuth(c)
|
||||
return
|
||||
}
|
||||
|
||||
c.Next()
|
||||
}
|
||||
claims["user"] = user
|
||||
}
|
||||
|
||||
c.Set("claims", claims)
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func ValidateJWT(jwtString, key string) (jwt.MapClaims, error) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue