Skip to content

Commit

Permalink
feat(jwt): 暴露jwt自定义配置
Browse files Browse the repository at this point in the history
  • Loading branch information
aide-cloud committed Apr 28, 2024
1 parent 499d46c commit ee522d1
Show file tree
Hide file tree
Showing 7 changed files with 319 additions and 184 deletions.
3 changes: 2 additions & 1 deletion app/prom_server/cmd/prom_server/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions app/prom_server/configs/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ server:
addr: 0.0.0.0:8888
timeout: 10s

jwt:
secret: secret
issuer: moon
expires: 8600s

# NOTE: 1.使用sqlite默认会在deploy/sql下生成init_sqlite.db数据库文件
# 2. 选择mysql, 把sqlite部分注释并创建名为prometheus_manager的数据库, 并配置主机:ip, 如:127.0.0.1:3306,下方redis配置同理
data:
Expand Down
1 change: 1 addition & 0 deletions app/prom_server/internal/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var ProviderSetConf = wire.NewSet(
wire.FieldsOf(new(*Bootstrap), "Log"),
wire.FieldsOf(new(*Bootstrap), "ApiWhite"),
wire.FieldsOf(new(*Bootstrap), "Interflow"),
wire.FieldsOf(new(*Bootstrap), "JWT"),
wire.Bind(new(plog.Config), new(*Log)),
LoadConfig,
)
Expand Down
439 changes: 267 additions & 172 deletions app/prom_server/internal/conf/conf.pb.go

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions app/prom_server/internal/conf/conf.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ message Bootstrap {
ApiWhite apiWhite = 5;
Email email = 6;
Interflow interflow = 7;
JWT jwt = 8;
}

message Server {
Expand Down Expand Up @@ -112,3 +113,9 @@ message Interflow {
Hook hook = 2;
MQ mq = 3;
}

message JWT {
string secret = 1;
string issuer = 2;
google.protobuf.Duration expires = 3;
}
20 changes: 12 additions & 8 deletions app/prom_server/internal/server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@ import (
"context"
nHttp "net/http"

"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/middleware/auth/jwt"
"github.com/go-kratos/kratos/v2/middleware/recovery"
"github.com/go-kratos/kratos/v2/middleware/selector"
"github.com/go-kratos/kratos/v2/middleware/validate"
"github.com/go-kratos/kratos/v2/transport/http"
jwtv4 "github.com/golang-jwt/jwt/v4"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/aide-family/moon/api/alarm/hook"
"github.com/aide-family/moon/api/interflows"
"github.com/aide-family/moon/api/ping"
Expand All @@ -37,6 +29,14 @@ import (
"github.com/aide-family/moon/pkg/helper/middler"
"github.com/aide-family/moon/pkg/helper/prom"
"github.com/aide-family/moon/pkg/servers"
"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/middleware/auth/jwt"
"github.com/go-kratos/kratos/v2/middleware/recovery"
"github.com/go-kratos/kratos/v2/middleware/selector"
"github.com/go-kratos/kratos/v2/middleware/validate"
"github.com/go-kratos/kratos/v2/transport/http"
jwtv4 "github.com/golang-jwt/jwt/v4"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

type HttpServer struct {
Expand Down Expand Up @@ -96,6 +96,7 @@ func RegisterHttpServer(
// NewHTTPServer new an HTTP server.
func NewHTTPServer(
c *conf.Server,
jwtConf *conf.JWT,
d *data.Data,
apiWhite *conf.ApiWhite,
logger log.Logger,
Expand All @@ -105,6 +106,9 @@ func NewHTTPServer(

jwt.WithSigningMethod(jwtv4.SigningMethodHS256)
jwt.WithClaims(func() jwtv4.Claims { return &jwtv4.RegisteredClaims{} })
middler.SetSecret(jwtConf.GetSecret())
middler.SetExpire(jwtConf.GetExpires().AsDuration())
middler.SetIssuer(jwtConf.GetIssuer())

allApi := apiWhite.GetAll()
jwtApis := append(allApi, apiWhite.GetJwtApi()...)
Expand Down
28 changes: 25 additions & 3 deletions pkg/helper/middler/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"encoding/json"
"time"

"github.com/aide-family/moon/pkg/util/cache"
"github.com/go-kratos/kratos/v2/middleware"
"github.com/go-kratos/kratos/v2/middleware/auth/jwt"
"github.com/go-kratos/kratos/v2/middleware/selector"
jwtv4 "github.com/golang-jwt/jwt/v4"
"github.com/aide-family/moon/pkg/util/cache"

"github.com/aide-family/moon/api/perrors"
"github.com/aide-family/moon/pkg/helper/consts"
Expand All @@ -24,7 +24,9 @@ type AuthClaims struct {
}

var (
secret = []byte("secret")
secret = []byte("secret")
issuer = "moon"
expires = 24 * time.Hour
)

var (
Expand Down Expand Up @@ -53,9 +55,28 @@ func (l *AuthClaims) String() string {

// SetSecret set secret
func SetSecret(s string) {
if s == "" {
return
}
secret = []byte(s)
}

// SetExpire 设置过期时间
func SetExpire(d time.Duration) {
if d <= 0 {
return
}
expires = d
}

// SetIssuer 设置issuer
func SetIssuer(i string) {
if i == "" {
return
}
issuer = i
}

// Expire 把token过期掉
func Expire(ctx context.Context, rdsClient cache.GlobalCache, authClaims *AuthClaims) error {
nowUnix := time.Now().Unix()
Expand Down Expand Up @@ -125,7 +146,7 @@ func IsAdminRole(ctx context.Context) bool {

// IssueToken issue token
func IssueToken(id, role uint32) (string, error) {
return IssueTokenWithDuration(id, role, time.Hour*24)
return IssueTokenWithDuration(id, role, expires)
}

// IssueTokenWithDuration issue token with duration
Expand All @@ -135,6 +156,7 @@ func IssueTokenWithDuration(id uint32, role uint32, duration time.Duration) (str
Role: role,
RegisteredClaims: &jwtv4.RegisteredClaims{
ExpiresAt: jwtv4.NewNumericDate(time.Now().Add(duration)),
Issuer: issuer,
},
}
token := jwtv4.NewWithClaims(jwtv4.SigningMethodHS256, claims)
Expand Down

0 comments on commit ee522d1

Please sign in to comment.