🐛 fixed the issue that third-party login sessions were missing

This commit is contained in:
2024-12-20 01:19:29 +08:00
parent 49831fc4d0
commit 40d073db0f
27 changed files with 556 additions and 308 deletions

View File

@@ -0,0 +1,6 @@
package constant
const (
JWT_TYPE_ACCESS string = "access"
JWT_TYPE_REFRESH string = "refresh"
)

View File

@@ -0,0 +1,19 @@
package errors
import "fmt"
// CodeMsg is a struct that contains a code and a message.
// It implements the error interface.
type CodeMsg struct {
Code int
Msg string
}
func (c *CodeMsg) Error() string {
return fmt.Sprintf("code: %d, msg: %s", c.Code, c.Msg)
}
// New creates a new CodeMsg.
func New(code int, msg string) error {
return &CodeMsg{Code: code, Msg: msg}
}

View File

@@ -0,0 +1,85 @@
package http
import (
"context"
"encoding/xml"
"net/http"
"schisandra-album-cloud-microservices/app/core/api/common/errors"
"github.com/zeromicro/go-zero/rest/httpx"
"google.golang.org/grpc/status"
)
// BaseResponse is the base response struct.
type BaseResponse[T any] struct {
// Code represents the business code, not the http status code.
Code int `json:"code" xml:"code"`
// Msg represents the business message, if Code = BusinessCodeOK,
// and Msg is empty, then the Msg will be set to BusinessMsgOk.
Msg string `json:"msg" xml:"msg"`
// Data represents the business data.
Data T `json:"data,omitempty" xml:"data,omitempty"`
}
type baseXmlResponse[T any] struct {
XMLName xml.Name `xml:"xml"`
Version string `xml:"version,attr"`
Encoding string `xml:"encoding,attr"`
BaseResponse[T]
}
// JsonBaseResponse writes v into w with http.StatusOK.
func JsonBaseResponse(w http.ResponseWriter, v any) {
httpx.OkJson(w, wrapBaseResponse(v))
}
// JsonBaseResponseCtx writes v into w with http.StatusOK.
func JsonBaseResponseCtx(ctx context.Context, w http.ResponseWriter, v any) {
httpx.OkJsonCtx(ctx, w, wrapBaseResponse(v))
}
// XmlBaseResponse writes v into w with http.StatusOK.
func XmlBaseResponse(w http.ResponseWriter, v any) {
OkXml(w, wrapXmlBaseResponse(v))
}
// XmlBaseResponseCtx writes v into w with http.StatusOK.
func XmlBaseResponseCtx(ctx context.Context, w http.ResponseWriter, v any) {
OkXmlCtx(ctx, w, wrapXmlBaseResponse(v))
}
func wrapXmlBaseResponse(v any) baseXmlResponse[any] {
base := wrapBaseResponse(v)
return baseXmlResponse[any]{
Version: xmlVersion,
Encoding: xmlEncoding,
BaseResponse: base,
}
}
func wrapBaseResponse(v any) BaseResponse[any] {
var resp BaseResponse[any]
switch data := v.(type) {
case *errors.CodeMsg:
resp.Code = data.Code
resp.Msg = data.Msg
case errors.CodeMsg:
resp.Code = data.Code
resp.Msg = data.Msg
case *status.Status:
resp.Code = int(data.Code())
resp.Msg = data.Message()
case interface{ GRPCStatus() *status.Status }:
resp.Code = int(data.GRPCStatus().Code())
resp.Msg = data.GRPCStatus().Message()
case error:
resp.Code = BusinessCodeError
resp.Msg = data.Error()
default:
resp.Code = BusinessCodeOK
resp.Msg = BusinessMsgOk
resp.Data = v
}
return resp
}

View File

@@ -0,0 +1,100 @@
package http
import (
"context"
"encoding/xml"
"fmt"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"net/http"
)
// OkXml writes v into w with 200 OK.
func OkXml(w http.ResponseWriter, v any) {
WriteXml(w, http.StatusOK, v)
}
// OkXmlCtx writes v into w with 200 OK.
func OkXmlCtx(ctx context.Context, w http.ResponseWriter, v any) {
WriteXmlCtx(ctx, w, http.StatusOK, v)
}
// WriteXml writes v as xml string into w with code.
func WriteXml(w http.ResponseWriter, code int, v any) {
if err := doWriteXml(w, code, v); err != nil {
logx.Error(err)
}
}
// WriteXmlCtx writes v as xml string into w with code.
func WriteXmlCtx(ctx context.Context, w http.ResponseWriter, code int, v any) {
if err := doWriteXml(w, code, v); err != nil {
logx.WithContext(ctx).Error(err)
}
}
func doWriteXml(w http.ResponseWriter, code int, v any) error {
bs, err := xml.Marshal(v)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return fmt.Errorf("marshal xml failed, error: %w", err)
}
w.Header().Set(httpx.ContentType, XmlContentType)
w.WriteHeader(code)
if n, err := w.Write(bs); err != nil {
// http.ErrHandlerTimeout has been handled by http.TimeoutHandler,
// so it's ignored here.
if err != http.ErrHandlerTimeout {
return fmt.Errorf("write response failed, error: %w", err)
}
} else if n < len(bs) {
return fmt.Errorf("actual bytes: %d, written bytes: %d", len(bs), n)
}
return nil
}
// OkHTML writes v into w with 200 OK.
func OkHTML(w http.ResponseWriter, v string) {
WriteHTML(w, http.StatusOK, v)
}
// OkHTMLCtx writes v into w with 200 OK.
func OkHTMLCtx(ctx context.Context, w http.ResponseWriter, v string) {
WriteHTMLCtx(ctx, w, http.StatusOK, v)
}
// WriteHTML writes v as HTML string into w with code.
func WriteHTML(w http.ResponseWriter, code int, v string) {
if err := doWriteHTML(w, code, v); err != nil {
logx.Error(err)
}
}
// WriteHTMLCtx writes v as HTML string into w with code.
func WriteHTMLCtx(ctx context.Context, w http.ResponseWriter, code int, v string) {
if err := doWriteHTML(w, code, v); err != nil {
logx.WithContext(ctx).Error(err)
}
}
func doWriteHTML(w http.ResponseWriter, code int, v string) error {
w.Header().Set(httpx.ContentType, HTMLContentType)
w.WriteHeader(code)
bs := []byte(v)
if n, err := w.Write(bs); err != nil {
// http.ErrHandlerTimeout has been handled by http.TimeoutHandler,
// so it's ignored here.
if err != http.ErrHandlerTimeout {
return fmt.Errorf("write response failed, error: %w", err)
}
} else if n < len(bs) {
return fmt.Errorf("actual bytes: %d, written bytes: %d", len(bs), n)
}
return nil
}

View File

@@ -0,0 +1,19 @@
package http
const (
xmlVersion = "1.0"
xmlEncoding = "UTF-8"
// BusinessCodeOK represents the business code for success.
BusinessCodeOK = 0
// BusinessMsgOk represents the business message for success.
BusinessMsgOk = "ok"
// BusinessCodeError represents the business code for error.
BusinessCodeError = -1
// XmlContentType represents the content type for xml.
XmlContentType = "application/xml"
// HTMLContentType represents the content type for html.
HTMLContentType = "text/html;charset=utf-8"
)

View File

@@ -8,7 +8,7 @@ import (
type AccessJWTPayload struct {
UserID string `json:"user_id"`
Type string `json:"type" default:"access"`
Type string `json:"type"`
}
type AccessJWTClaims struct {
AccessJWTPayload
@@ -19,7 +19,7 @@ func GenerateAccessToken(secret string, payload AccessJWTPayload) string {
claims := AccessJWTClaims{
AccessJWTPayload: payload,
RegisteredClaims: jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Minute * 15)),
ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Minute * 30)),
IssuedAt: jwt.NewNumericDate(time.Now()),
NotBefore: jwt.NewNumericDate(time.Now()),
},

View File

@@ -8,7 +8,7 @@ import (
type RefreshJWTPayload struct {
UserID string `json:"user_id"`
Type string `json:"type" default:"refresh"`
Type string `json:"type"`
}
type RefreshJWTClaims struct {
RefreshJWTPayload