mirror of
https://github.com/xiaoxinpro/nginx-proxy-manager-zh.git
synced 2025-01-23 21:28:15 -05:00
215083f6cf
- Certificate renewal is just a re-request as it's forced already - Rejig the routes for readability - Added Server Side Events so that the UI would invalidate the cache when changes happen on the backend, such as certs being provided or failing - Added a SSE Token, which has the same shelf life as normal token but can't be used interchangeably. The reason for this is, the SSE endpoint needs a token for auth as a Query param, so it would be stored in log files. If someone where to get a hold of that, it's pretty useless as it can't be used to change anything, only to listen for events until it expires - Added test endpoint for SSE testing only availabe in debug mode
66 lines
1.4 KiB
Go
66 lines
1.4 KiB
Go
package jwt
|
|
|
|
import (
|
|
"time"
|
|
|
|
"npm/internal/entity/user"
|
|
"npm/internal/logger"
|
|
|
|
"github.com/dgrijalva/jwt-go"
|
|
"github.com/rotisserie/eris"
|
|
)
|
|
|
|
// UserJWTClaims is the structure of a JWT for a User
|
|
type UserJWTClaims struct {
|
|
UserID int `json:"uid"`
|
|
Roles []string `json:"roles"`
|
|
jwt.StandardClaims
|
|
}
|
|
|
|
// GeneratedResponse is the response of a generated token, usually used in http response
|
|
type GeneratedResponse struct {
|
|
Expires int64 `json:"expires"`
|
|
Token string `json:"token"`
|
|
}
|
|
|
|
// Generate will create a JWT
|
|
func Generate(userObj *user.Model, forSSE bool) (GeneratedResponse, error) {
|
|
var response GeneratedResponse
|
|
|
|
key, _ := GetPrivateKey()
|
|
expires := time.Now().AddDate(0, 0, 1) // 1 day
|
|
issuer := "api"
|
|
|
|
if forSSE {
|
|
issuer = "sse"
|
|
}
|
|
|
|
// Create the Claims
|
|
claims := UserJWTClaims{
|
|
userObj.ID,
|
|
[]string{"user"},
|
|
jwt.StandardClaims{
|
|
IssuedAt: time.Now().Unix(),
|
|
ExpiresAt: expires.Unix(),
|
|
Issuer: issuer,
|
|
},
|
|
}
|
|
|
|
// Create a new token object, specifying signing method and the claims
|
|
// you would like it to contain.
|
|
token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
|
|
var err error
|
|
token.Signature, err = token.SignedString(key)
|
|
if err != nil {
|
|
logger.Error("JWTError", eris.Wrapf(err, "Error signing token: %v", err))
|
|
return response, err
|
|
}
|
|
|
|
response = GeneratedResponse{
|
|
Expires: expires.Unix(),
|
|
Token: token.Signature,
|
|
}
|
|
|
|
return response, nil
|
|
}
|