-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: ShuangxueWu <qingchoulove@hotmail.com>
- Loading branch information
1 parent
b3bcd88
commit eeb5471
Showing
6 changed files
with
197 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package handler | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/openapp-dev/openapp/pkg/utils" | ||
"k8s.io/klog" | ||
) | ||
|
||
type loginResponse struct { | ||
Token string `json:"token"` | ||
} | ||
|
||
func LoginHandler(ctx *gin.Context) { | ||
klog.V(4).Info("Start to login...") | ||
openappHelper, err := getOpenAPPHelper(ctx) | ||
if err != nil { | ||
klog.Errorf("Failed to get openapp lister: %v", err) | ||
returnFormattedData(ctx, http.StatusInternalServerError, "Failed to get openapp lister", nil) | ||
return | ||
} | ||
json := make(map[string]interface{}) | ||
err = ctx.BindJSON(&json) | ||
if err != nil { | ||
klog.Errorf("Failed to bind json: %v", err) | ||
returnFormattedData(ctx, http.StatusBadRequest, "Failed to bind json", nil) | ||
return | ||
} | ||
username, ok := json["username"].(string) | ||
if !ok { | ||
klog.Errorf("Failed to get username from json") | ||
returnFormattedData(ctx, http.StatusBadRequest, "Failed to get username from json", nil) | ||
return | ||
} | ||
password, ok := json["password"].(string) | ||
if !ok { | ||
klog.Errorf("Failed to get password from json") | ||
returnFormattedData(ctx, http.StatusBadRequest, "Failed to get password from json", nil) | ||
return | ||
} | ||
cfg, err := openappHelper.ConfigMapLister.ConfigMaps(utils.SystemNamespace).Get(utils.SystemConfigMap) | ||
if err != nil { | ||
klog.Errorf("Failed to get config: %v", err) | ||
returnFormattedData(ctx, http.StatusInternalServerError, "Failed to get config", nil) | ||
return | ||
} | ||
if cfg.Data["username"] != username || cfg.Data["password"] != password { | ||
klog.Errorf("Failed to login") | ||
returnFormattedData(ctx, http.StatusUnauthorized, "Failed to login", nil) | ||
return | ||
} | ||
token, err := utils.NewJWT([]byte(cfg.Data["password"])).GenerateToken(username, password) | ||
if err != nil { | ||
klog.Errorf("Failed to generate token: %v", err) | ||
returnFormattedData(ctx, http.StatusInternalServerError, "Failed to generate token", nil) | ||
return | ||
} | ||
returnFormattedData(ctx, http.StatusOK, "Login successfully", &loginResponse{Token: token}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package utils | ||
|
||
import ( | ||
"time" | ||
|
||
jwt "github.com/golang-jwt/jwt/v5" | ||
) | ||
|
||
type Claims struct { | ||
Username string `json:"username"` | ||
Password string `json:"password"` | ||
jwt.RegisteredClaims | ||
} | ||
|
||
type JWT struct { | ||
secret []byte | ||
} | ||
|
||
func NewJWT(secret []byte) *JWT { | ||
return &JWT{secret: secret} | ||
} | ||
|
||
func (j *JWT) GenerateToken(username, password string) (string, error) { | ||
claims := Claims{ | ||
Username: username, | ||
Password: password, | ||
RegisteredClaims: jwt.RegisteredClaims{ | ||
ExpiresAt: jwt.NewNumericDate(time.Now().Add(24 * time.Hour)), | ||
IssuedAt: jwt.NewNumericDate(time.Now()), | ||
NotBefore: jwt.NewNumericDate(time.Now()), | ||
Issuer: "openapp", | ||
}, | ||
} | ||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) | ||
return token.SignedString(j.secret) | ||
} | ||
|
||
func (j *JWT) ParseToken(tokenString string) (*Claims, error) { | ||
token, err := jwt.ParseWithClaims(tokenString, &Claims{}, func(token *jwt.Token) (interface{}, error) { | ||
return j.secret, nil | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
claims, ok := token.Claims.(*Claims) | ||
if ok && token.Valid { | ||
return claims, nil | ||
} | ||
return nil, err | ||
} |