Skip to content

Commit c0e0ae5

Browse files
committed
refactor: decouple errors from gmicro package in the jwt logic
1 parent ceabcb1 commit c0e0ae5

File tree

12 files changed

+1105
-166
lines changed

12 files changed

+1105
-166
lines changed

gmicro/code/base.go renamed to app/pkg/code/base.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@ const (
2525
ErrPageNotFound
2626
)
2727

28-
// common: database errors.
29-
const (
30-
// ErrDatabase - 500: Database error.
31-
ErrDatabase int = iota + 100101
32-
)
33-
3428
// common: authorization and authentication errors.
3529
const (
3630
// ErrEncrypt - 401: Error occurred while encrypting the user password.

app/pkg/code/db.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package code
22

33
//go:generate codeg -type=int
44
const (
5-
// ErrConnectDB - 500: Init db error.
6-
ErrConnectDB int = iota + 100101
5+
// ErrDatabase - 500: Database error.
6+
ErrDatabase int = iota + 100101
77

8+
// ErrConnectDB - 500: Init db error.
9+
ErrConnectDB
810
// ErrConnectGRPC - 500: Connect to grpc error.
911
ErrConnectGRPC
1012
)

app/pkg/code/error_code_generated.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,24 @@
2121

2222
| Identifier | Code | HTTP Code | Description |
2323
| ---------- | ---- | --------- | ----------- |
24-
| ErrConnectDB | 100601 | 500 | Init db error |
25-
| ErrConnectGRPC | 100602 | 500 | Connect to grpc error |
24+
| ErrEncrypt | 100201 | 401 | Error occurred while encrypting the user password |
25+
| ErrSignatureInvalid | 100202 | 401 | Signature is invalid |
26+
| ErrExpired | 100203 | 401 | Token expired |
27+
| ErrInvalidAuthHeader | 100204 | 401 | Invalid authorization header |
28+
| ErrMissingHeader | 100205 | 401 | The `Authorization` header was empty |
29+
| ErrPasswordIncorrect | 100206 | 401 | Password was incorrect |
30+
| ErrPermissionDenied | 100207 | 403 | Permission denied |
31+
| ErrEncodingFailed | 100301 | 500 | Encoding failed due to an error with the data |
32+
| ErrDecodingFailed | 100302 | 500 | Decoding failed due to an error with the data |
33+
| ErrInvalidJSON | 100303 | 500 | Data is not valid JSON |
34+
| ErrEncodingJSON | 100304 | 500 | JSON data could not be encoded |
35+
| ErrDecodingJSON | 100305 | 500 | JSON data could not be decoded |
36+
| ErrInvalidYaml | 100306 | 500 | Data is not valid Yaml |
37+
| ErrEncodingYaml | 100307 | 500 | Yaml data could not be encoded |
38+
| ErrDecodingYaml | 100308 | 500 | Yaml data could not be decoded |
39+
| ErrDatabase | 100101 | 500 | Database error |
40+
| ErrConnectDB | 100102 | 500 | Init db error |
41+
| ErrConnectGRPC | 100103 | 500 | Connect to grpc error |
2642
| ErrGoodsNotFound | 100501 | 404 | Goods not found |
2743
| ErrCategoryNotFound | 100502 | 404 | Category not found |
2844
| ErrEsUnmarshal | 100503 | 500 | Es unmarshal error |

gmicro/code/code.go

Lines changed: 0 additions & 62 deletions
This file was deleted.

gmicro/code/code_generated.go

Lines changed: 0 additions & 25 deletions
This file was deleted.

gmicro/code/error_code_generated.md

Lines changed: 0 additions & 40 deletions
This file was deleted.

gmicro/server/restserver/middlewares/auth/auto.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package auth
22

33
import (
4+
"net/http"
45
"strings"
56

67
"github.com/coderi421/gframework/pkg/common/core"
78

8-
"github.com/coderi421/gframework/gmicro/code"
99
"github.com/coderi421/gframework/gmicro/server/restserver/middlewares"
1010

11-
"github.com/coderi421/gframework/pkg/errors"
12-
1311
"github.com/gin-gonic/gin"
1412
)
1513

@@ -39,11 +37,15 @@ func (a AutoStrategy) AuthFunc() gin.HandlerFunc {
3937
authHeader := strings.SplitN(c.Request.Header.Get("Authorization"), " ", 2)
4038

4139
if len(authHeader) != authHeaderCount {
42-
core.WriteResponse(
43-
c,
44-
errors.WithCode(code.ErrInvalidAuthHeader, "Authorization header format is wrong."),
45-
nil,
46-
)
40+
//core.WriteResponse(
41+
// c,
42+
// errors.WithCode(code.ErrInvalidAuthHeader, "Authorization header format is wrong."),
43+
// nil,
44+
//)
45+
46+
c.JSON(http.StatusUnauthorized, core.ErrResponse{
47+
Message: "Invalid authorization header.",
48+
})
4749
c.Abort()
4850

4951
return
@@ -56,7 +58,10 @@ func (a AutoStrategy) AuthFunc() gin.HandlerFunc {
5658
operator.SetStrategy(a.jwt)
5759
// a.JWT.MiddlewareFunc()(c)
5860
default:
59-
core.WriteResponse(c, errors.WithCode(code.ErrSignatureInvalid, "unrecognized Authorization header."), nil)
61+
//core.WriteResponse(c, errors.WithCode(code.ErrSignatureInvalid, "unrecognized Authorization header."), nil)
62+
c.JSON(http.StatusUnauthorized, core.ErrResponse{
63+
Message: "unrecognized Authorization header.",
64+
})
6065
c.Abort()
6166

6267
return

gmicro/server/restserver/middlewares/auth/basic.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@ package auth
22

33
import (
44
"encoding/base64"
5+
"net/http"
56
"strings"
67

78
"github.com/coderi421/gframework/pkg/common/core"
89

9-
"github.com/coderi421/gframework/gmicro/code"
1010
"github.com/coderi421/gframework/gmicro/server/restserver/middlewares"
1111

12-
"github.com/coderi421/gframework/pkg/errors"
13-
1412
"github.com/gin-gonic/gin"
1513
)
1614

@@ -32,13 +30,16 @@ func NewBasicStrategy(compare func(username string, password string) bool) Basic
3230
func (b BasicStrategy) AuthFunc() gin.HandlerFunc {
3331
return func(c *gin.Context) {
3432
auth := strings.SplitN(c.Request.Header.Get("Authorization"), " ", 2)
35-
3633
if len(auth) != 2 || auth[0] != "Basic" {
37-
core.WriteResponse(
38-
c,
39-
errors.WithCode(code.ErrSignatureInvalid, "Authorization header format is wrong."),
40-
nil,
41-
)
34+
//core.WriteResponse(
35+
// c,
36+
// errors.WithCode(code.ErrSignatureInvalid, "Authorization header format is wrong."),
37+
// nil,
38+
//)
39+
40+
c.JSON(http.StatusUnauthorized, core.ErrResponse{
41+
Message: "Authorization header format is wrong.",
42+
})
4243
c.Abort()
4344

4445
return
@@ -49,11 +50,14 @@ func (b BasicStrategy) AuthFunc() gin.HandlerFunc {
4950

5051
//在这一步实现自己注入的compare
5152
if len(pair) != 2 || !b.compare(pair[0], pair[1]) {
52-
core.WriteResponse(
53-
c,
54-
errors.WithCode(code.ErrSignatureInvalid, "Authorization header format is wrong."),
55-
nil,
56-
)
53+
//core.WriteResponse(
54+
// c,
55+
// errors.WithCode(code.ErrSignatureInvalid, "Authorization header format is wrong."),
56+
// nil,
57+
//)
58+
c.JSON(http.StatusUnauthorized, core.ErrResponse{
59+
Message: "Authorization header format is wrong.",
60+
})
5761
c.Abort()
5862

5963
return

gmicro/server/restserver/middlewares/auth/cache.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package auth
22

33
import (
44
"fmt"
5+
"net/http"
56
"time"
67

78
jwt "github.com/dgrijalva/jwt-go/v4"
89

9-
"github.com/coderi421/gframework/gmicro/code"
1010
"github.com/coderi421/gframework/gmicro/server/restserver/middlewares"
1111
"github.com/coderi421/gframework/pkg/common/core"
1212
"github.com/coderi421/gframework/pkg/errors"
@@ -46,7 +46,10 @@ func (cache CacheStrategy) AuthFunc() gin.HandlerFunc {
4646
return func(c *gin.Context) {
4747
header := c.Request.Header.Get("Authorization")
4848
if len(header) == 0 {
49-
core.WriteResponse(c, errors.WithCode(code.ErrMissingHeader, "Authorization header cannot be empty."), nil)
49+
//core.WriteResponse(c, errors.WithCode(code.ErrMissingHeader, "Authorization header cannot be empty."), nil)
50+
c.JSON(http.StatusUnauthorized, core.ErrResponse{
51+
Message: "Authorization header cannot be empty.",
52+
})
5053
c.Abort()
5154

5255
return
@@ -84,15 +87,21 @@ func (cache CacheStrategy) AuthFunc() gin.HandlerFunc {
8487
return []byte(secret.Key), nil
8588
}, jwt.WithAudience(AuthzAudience))
8689
if err != nil || !parsedT.Valid {
87-
core.WriteResponse(c, errors.WithCode(code.ErrSignatureInvalid, err.Error()), nil)
90+
c.JSON(http.StatusUnauthorized, core.ErrResponse{
91+
Message: err.Error(),
92+
})
93+
//core.WriteResponse(c, errors.WithCode(code.ErrSignatureInvalid, err.Error()), nil)
8894
c.Abort()
8995

9096
return
9197
}
9298

9399
if KeyExpired(secret.Expires) {
94100
tm := time.Unix(secret.Expires, 0).Format("2006-01-02 15:04:05")
95-
core.WriteResponse(c, errors.WithCode(code.ErrExpired, "expired at: %s", tm), nil)
101+
c.JSON(http.StatusUnauthorized, core.ErrResponse{
102+
Message: fmt.Sprintf("expired at: %s", tm),
103+
})
104+
//core.WriteResponse(c, errors.WithCode(code.ErrExpired, "expired at: %s", tm), nil)
96105
c.Abort()
97106

98107
return

gmicro/server/restserver/middlewares/auth/jwt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
// AuthzAudience defines the value of jwt audience field.
11-
const AuthzAudience = "mxshop.imooc.com"
11+
const AuthzAudience = "gmicro.com"
1212

1313
// JWTStrategy defines jwt bearer authentication strategy.
1414
type JWTStrategy struct {

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ require (
2121
cloud.google.com/go/firestore v1.9.0 // indirect
2222
cloud.google.com/go/longrunning v0.4.1 // indirect
2323
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
24+
github.com/PaulXu-cn/go-mod-graph-chart v0.5.3 // indirect
25+
github.com/appleboy/gin-jwt/v2 v2.9.1 // indirect
2426
github.com/armon/go-metrics v0.4.0 // indirect
2527
github.com/beorn7/perks v1.0.1 // indirect
2628
github.com/bits-and-blooms/bitset v1.2.0 // indirect
2729
github.com/bytedance/sonic v1.8.0 // indirect
2830
github.com/cespare/xxhash/v2 v2.2.0 // indirect
2931
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
3032
github.com/coreos/go-semver v0.3.0 // indirect
31-
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
3233
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
3334
github.com/davecgh/go-spew v1.1.1 // indirect
3435
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
@@ -47,6 +48,7 @@ require (
4748
github.com/go-sql-driver/mysql v1.7.0 // indirect
4849
github.com/goccy/go-json v0.10.0 // indirect
4950
github.com/gogo/protobuf v1.3.2 // indirect
51+
github.com/golang-jwt/jwt/v4 v4.4.3 // indirect
5052
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
5153
github.com/golang/protobuf v1.5.3 // indirect
5254
github.com/google/go-cmp v0.5.9 // indirect

0 commit comments

Comments
 (0)