Skip to content

Commit

Permalink
Upgrade 1.8.0 (#19)
Browse files Browse the repository at this point in the history
* Upgrade v1.8.0
  • Loading branch information
hwbrzzl authored Jan 30, 2023
1 parent ad2933f commit 69db9b9
Show file tree
Hide file tree
Showing 122 changed files with 8,155 additions and 1,670 deletions.
27 changes: 27 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
MAIL_HOST=
MAIL_PORT=
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_FROM_ADDRESS=
MAIL_FROM_NAME=

MAIL_TO=
MAIL_CC=
MAIL_BCC=

AWS_ACCESS_KEY_ID=
AWS_ACCESS_KEY_SECRET=
AWS_DEFAULT_REGION=
AWS_BUCKET=
AWS_URL=

ALIYUN_ACCESS_KEY_ID=
ALIYUN_ACCESS_KEY_SECRET=
ALIYUN_BUCKET=
ALIYUN_URL=
ALIYUN_ENDPOINT=

TENCENT_ACCESS_KEY_ID=
TENCENT_ACCESS_KEY_SECRET=
TENCENT_BUCKET=
TENCENT_URL=
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
go.sum
config/.env
.env
.idea
.DS_Store
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<p align="center"><img src="https://goravel.s3.us-east-2.amazonaws.com/goravel-word.png" width="300"></p>
<p align="center"><img src="logo.png" width="300"></p>

English | [中文](./README_zh.md)

Expand Down Expand Up @@ -29,10 +29,9 @@ Golang developers quickly build their own applications.

## Roadmap

- [ ] Optimize migration
- [ ] Orm relationships
- [ ] Custom .env path
- [ ] Database read-write separation
- [ ] Extend Redis driver

## Documentation

Expand Down
5 changes: 2 additions & 3 deletions README_zh.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<p align="center"><img src="https://goravel.s3.us-east-2.amazonaws.com/goravel-word.png" width="300"></p>
<p align="center"><img src="logo.png" width="300"></p>

[English](./README.md) | 中文

Expand Down Expand Up @@ -28,10 +28,9 @@ Goravel 是一个功能完备、具有良好扩展能力的 Web 应用程序框

## 路线图

- [ ] 优化迁移
- [ ] Orm 关联关系
- [ ] 自定义 .env 路径
- [ ] 数据库读写分离
- [ ] 扩展 Redis 驱动

## 文档

Expand Down
44 changes: 14 additions & 30 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package auth

import (
"errors"
"reflect"
"strings"
"time"

contractauth "github.com/goravel/framework/contracts/auth"
"github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/facades"
"github.com/goravel/framework/support/database"
supporttime "github.com/goravel/framework/support/time"

"github.com/golang-jwt/jwt/v4"
Expand All @@ -20,15 +20,6 @@ const ctxKey = "GoravelAuth"

var (
unit = time.Minute

ErrorRefreshTimeExceeded = errors.New("refresh time exceeded")
ErrorTokenExpired = errors.New("token expired")
ErrorNoPrimaryKeyField = errors.New("the primaryKey field was not found in the model, set primaryKey like orm.Model")
ErrorEmptySecret = errors.New("secret is required")
ErrorTokenDisabled = errors.New("token is disabled")
ErrorParseTokenFirst = errors.New("parse token first")
ErrorInvalidClaims = errors.New("invalid claims")
ErrorInvalidToken = errors.New("invalid token")
)

type Claims struct {
Expand All @@ -47,7 +38,7 @@ type Auth struct {
guard string
}

func NewAuth(guard string) contractauth.Auth {
func NewAuth(guard string) *Auth {
return &Auth{
guard: guard,
}
Expand All @@ -66,10 +57,12 @@ func (app *Auth) User(ctx http.Context, user any) error {
if auth[app.guard].Claims == nil {
return ErrorParseTokenFirst
}
if auth[app.guard].Claims.Key == "" {
return ErrorInvalidKey
}
if auth[app.guard].Token == "" {
return ErrorTokenExpired
}
//todo Unit test
if err := facades.Orm.Query().Find(user, clause.Eq{Column: clause.PrimaryColumn, Value: auth[app.guard].Claims.Key}); err != nil {
return err
}
Expand Down Expand Up @@ -116,25 +109,12 @@ func (app *Auth) Parse(ctx http.Context, token string) error {
}

func (app *Auth) Login(ctx http.Context, user any) (token string, err error) {
t := reflect.TypeOf(user).Elem()
v := reflect.ValueOf(user).Elem()
for i := 0; i < t.NumField(); i++ {
if t.Field(i).Name == "Model" {
if v.Field(i).Type().Kind() == reflect.Struct {
structField := v.Field(i).Type()
for j := 0; j < structField.NumField(); j++ {
if structField.Field(j).Tag.Get("gorm") == "primaryKey" {
return app.LoginUsingID(ctx, v.Field(i).Field(j).Interface())
}
}
}
}
if t.Field(i).Tag.Get("gorm") == "primaryKey" {
return app.LoginUsingID(ctx, v.Field(i).Interface())
}
id := database.GetID(user)
if id == nil {
return "", ErrorNoPrimaryKeyField
}

return "", ErrorNoPrimaryKeyField
return app.LoginUsingID(ctx, id)
}

func (app *Auth) LoginUsingID(ctx http.Context, id any) (token string, err error) {
Expand All @@ -146,8 +126,12 @@ func (app *Auth) LoginUsingID(ctx http.Context, id any) (token string, err error
nowTime := supporttime.Now()
ttl := facades.Config.GetInt("jwt.ttl")
expireTime := nowTime.Add(time.Duration(ttl) * unit)
key := cast.ToString(id)
if key == "" {
return "", ErrorInvalidKey
}
claims := Claims{
cast.ToString(id),
key,
jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(expireTime),
IssuedAt: jwt.NewNumericDate(nowTime),
Expand Down
Loading

0 comments on commit 69db9b9

Please sign in to comment.