Skip to content

Commit

Permalink
👋 my geek time
Browse files Browse the repository at this point in the history
  • Loading branch information
zkep committed Dec 16, 2024
1 parent cec4622 commit 6c197ee
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 107 deletions.
75 changes: 50 additions & 25 deletions internal/api/v2/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package v2
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
Expand All @@ -20,7 +21,6 @@ import (
"github.com/golang-jwt/jwt/v4"
"github.com/tebeka/selenium"
"github.com/zkep/mygeektime/internal/global"
"github.com/zkep/mygeektime/internal/middleware"
"github.com/zkep/mygeektime/internal/model"
"github.com/zkep/mygeektime/internal/types/base"
"github.com/zkep/mygeektime/internal/types/geek"
Expand Down Expand Up @@ -49,24 +49,6 @@ func (b *Base) Login(c *gin.Context) {
var user model.User
switch r.Type {
case geek.LoginWithUser:
//if err := global.DB.
// Where(model.User{
// Phone: r.Account,
// }).
// First(&user).Error; err != nil {
// c.JSON(http.StatusOK, gin.H{
// "status": http.StatusBadRequest,
// "msg": "Login Fail",
// })
// return
//}
//if user.Phone == "" {
// c.JSON(http.StatusOK, gin.H{
// "status": http.StatusBadRequest,
// "msg": "not fund user",
// })
// return
//}
if err := loginWithAccount(r); err != nil {
c.JSON(http.StatusOK, gin.H{
"status": http.StatusBadRequest,
Expand Down Expand Up @@ -110,7 +92,8 @@ func (b *Base) Login(c *gin.Context) {
token, expire, err := global.JWT.DefaultTokenGenerator(
func() (jwt.MapClaims, error) {
claims := jwt.MapClaims{}
claims[middleware.Identity] = user.Uid
claims[global.Identity] = user.Uid
claims[global.AccessToken] = user.AccessToken
return claims, nil
})
if err != nil {
Expand All @@ -127,8 +110,51 @@ func (b *Base) Login(c *gin.Context) {
"user": user,
"expire": expire.Format(time.RFC3339),
})
c.SetCookie("analogjwt", user.AccessToken,
int(expire.Unix()), "/", "", false, false)
c.SetCookie(global.Analogjwt, token, int(expire.Unix()), "/", "", false, false)
}

func (b *Base) Redirect(c *gin.Context) {
var r base.RedirectRequest
if err := c.ShouldBind(&r); err != nil {
c.JSON(http.StatusOK, gin.H{
"status": http.StatusBadRequest,
"msg": "Login Fail",
})
return
}
tokenByte, err := base64.URLEncoding.DecodeString(r.Token)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"status": http.StatusBadRequest,
"msg": "Login Fail",
})
return
}
cookieToken := string(tokenByte)
var auth geek.AuthResponse
if err = authority(cookieToken, saveCookie(cookieToken, &auth)); err != nil {
c.JSON(http.StatusOK, gin.H{
"status": http.StatusBadRequest,
"msg": err.Error(),
})
return
}
token, expire, err := global.JWT.DefaultTokenGenerator(
func() (jwt.MapClaims, error) {
claims := jwt.MapClaims{}
claims[global.Identity] = auth.Data.UID
claims[global.AccessToken] = cookieToken
return claims, nil
})
if err != nil {
c.JSON(http.StatusOK, gin.H{
"status": http.StatusBadRequest,
"msg": "Login Fail",
})
return
}
c.SetCookie(global.Analogjwt, token, int(expire.Unix()), "/", "", false, false)
c.Redirect(http.StatusFound, "/")
}

const (
Expand Down Expand Up @@ -279,11 +305,10 @@ func loginWithSimulate() error {
if err = authority(cookiesLine, saveCookie(cookiesLine, &auth)); err != nil {
return false, err
}

addr := fmt.Sprintf("%s:%d", global.CONF.Server.HTTPAddr, global.CONF.Server.HTTPPort)
openURL := fmt.Sprintf("http://%s", addr)
token := base64.URLEncoding.EncodeToString([]byte(cookiesLine))
openURL := fmt.Sprintf("http://%s/%s?token=%s", addr, "v2/base/redirect", token)
_ = browser.Open(openURL)

return true, nil
}

Expand Down
17 changes: 8 additions & 9 deletions internal/api/v2/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/gin-gonic/gin"
"github.com/zkep/mygeektime/internal/global"
"github.com/zkep/mygeektime/internal/middleware"
"github.com/zkep/mygeektime/internal/model"
"github.com/zkep/mygeektime/internal/service"
"github.com/zkep/mygeektime/internal/types/geek"
Expand All @@ -35,8 +34,8 @@ func (p *Product) List(c *gin.Context) {
if req.Prev < 0 {
req.Prev = 0
}
identity := c.GetString(middleware.Identity)
accessToken := c.GetString(middleware.AccessToken)
identity := c.GetString(global.Identity)
accessToken := c.GetString(global.AccessToken)
resp, err := service.GetLearnProduct(c, identity, accessToken, req)
if err != nil {
c.JSON(http.StatusOK, gin.H{"status": 100, "msg": err.Error()})
Expand Down Expand Up @@ -79,8 +78,8 @@ func (p *Product) Articles(c *gin.Context) {
}
req.Size = req.PerPage
req.Prev = req.Page
identity := c.GetString(middleware.Identity)
accessToken := c.GetString(middleware.AccessToken)
identity := c.GetString(global.Identity)
accessToken := c.GetString(global.AccessToken)
resp, err := service.GetArticles(c, identity, accessToken, req)
if err != nil {
c.JSON(http.StatusOK, gin.H{"status": 100, "msg": err.Error()})
Expand Down Expand Up @@ -115,8 +114,8 @@ func (p *Product) ArticleInfo(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": 100, "msg": err.Error()})
return
}
identity := c.GetString(middleware.Identity)
accessToken := c.GetString(middleware.AccessToken)
identity := c.GetString(global.Identity)
accessToken := c.GetString(global.AccessToken)
resp, err := service.GetArticleInfo(c, identity, accessToken, req)
if err != nil {
c.JSON(http.StatusOK, gin.H{"status": 100, "msg": err.Error()})
Expand All @@ -131,8 +130,8 @@ func (p *Product) Download(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": 100, "error": err.Error()})
return
}
identity := c.GetString(middleware.Identity)
accessToken := c.GetString(middleware.AccessToken)
identity := c.GetString(global.Identity)
accessToken := c.GetString(global.AccessToken)
articlesMap := make(map[int64]*model.Article, 10)
ids := make([]int64, 0, 1)
switch x := req.Ids.(type) {
Expand Down
6 changes: 3 additions & 3 deletions internal/api/v2/pvip.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/zkep/mygeektime/internal/middleware"
"github.com/zkep/mygeektime/internal/global"
"github.com/zkep/mygeektime/internal/service"
"github.com/zkep/mygeektime/internal/types/geek"
)
Expand All @@ -23,8 +23,8 @@ func (p *Product) PvipProductList(c *gin.Context) {
if req.Prev < 0 {
req.Prev = 0
}
identity := c.GetString(middleware.Identity)
accessToken := c.GetString(middleware.AccessToken)
identity := c.GetString(global.Identity)
accessToken := c.GetString(global.AccessToken)
resp, err := service.GetPvipProduct(c, identity, accessToken, req)
if err != nil {
c.JSON(http.StatusOK, gin.H{"status": 100, "msg": err.Error()})
Expand Down
3 changes: 1 addition & 2 deletions internal/api/v2/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
md "github.com/JohannesKaufmann/html-to-markdown"
"github.com/gin-gonic/gin"
"github.com/zkep/mygeektime/internal/global"
"github.com/zkep/mygeektime/internal/middleware"
"github.com/zkep/mygeektime/internal/model"
"github.com/zkep/mygeektime/internal/service"
"github.com/zkep/mygeektime/internal/types/geek"
Expand All @@ -36,7 +35,7 @@ func (t *Task) List(c *gin.Context) {
if req.Page <= 0 {
req.Page = 1
}
identity := c.GetString(middleware.Identity)
identity := c.GetString(global.Identity)
ret := task.TaskListResponse{
Rows: make([]task.Task, 0, 10),
}
Expand Down
6 changes: 6 additions & 0 deletions internal/global/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ package global

import "github.com/zkep/mygeektime/lib/rest"

const (
Identity = "identity"
AccessToken = "access_token"
Analogjwt = "analogjwt"
)

var (
JWT *rest.JWTConfig
)
12 changes: 3 additions & 9 deletions internal/middleware/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ import (
"github.com/zkep/mygeektime/lib/rest"
)

const (
Identity = "identity"
AccessToken = "access_token"
)

func JWTMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
token, err := ParseToken(global.JWT, c)
Expand Down Expand Up @@ -59,8 +54,8 @@ func doWithToken(c *gin.Context, token *jwt.Token) error {
c.AbortWithStatusJSON(http.StatusOK, gin.H{"status": http.StatusBadRequest, "msg": "token is expired"})
return ErrExpiredToken
}
c.Set(Identity, claims[Identity])
c.Set(AccessToken, c.Request.Header.Get("Cookie"))
c.Set(global.Identity, claims[global.Identity])
c.Set(global.AccessToken, claims[global.AccessToken])
return nil
}

Expand All @@ -74,8 +69,7 @@ func ParseToken(j *rest.JWTConfig, c *gin.Context) (*jwt.Token, error) {
break
}
parts := strings.Split(strings.TrimSpace(method), ":")
k := strings.TrimSpace(parts[0])
v := strings.TrimSpace(parts[1])
k, v := strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1])
switch k {
case "header":
token, err = jwtFromHeader(c, v)
Expand Down
1 change: 1 addition & 0 deletions internal/router/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ func base(public, _ *gin.RouterGroup) {
api := v2.NewBase()
{
public.POST("/base/login", api.Login)
public.GET("/base/redirect", api.Redirect)
}
}
4 changes: 4 additions & 0 deletions internal/types/base/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ type LoginRequest struct {
Password string `json:"password,omitempty" validate:"min=5,max=50"`
Type string `json:"type,omitempty" validate:"min=5,max=50"`
}

type RedirectRequest struct {
Token string `form:"token,omitempty" validate:"min=5"`
}
2 changes: 1 addition & 1 deletion lib/rest/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const (
var (
DefaultJWTConfig = JWTConfig{
SigningMethod: AlgorithmHS256,
TokenLookup: "header:Authorization",
TokenLookup: "header:Authorization,cookie:analogjwt",
AuthScheme: "Bearer",
Claims: jwt.MapClaims{},
SigningKeys: map[string]any{},
Expand Down
Loading

0 comments on commit 6c197ee

Please sign in to comment.