forked from ixre/go2o
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
84 changed files
with
2,814 additions
and
1,481 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package api | ||
|
||
import ( | ||
"github.com/ixre/gof" | ||
"github.com/ixre/gof/crypto" | ||
api "github.com/ixre/gof/jwt-api" | ||
"time" | ||
) | ||
|
||
var _ api.Handler = new(AccessTokenApi) | ||
|
||
type AccessTokenApi struct { | ||
} | ||
|
||
func (a AccessTokenApi) Group() string { | ||
return "access_token" | ||
} | ||
|
||
func (a AccessTokenApi) Process(fn string, ctx api.Context) *api.Response { | ||
return a.createAccessToken(ctx) | ||
} | ||
|
||
func (a AccessTokenApi) createAccessToken(ctx api.Context) *api.Response { | ||
ownerKey := ctx.Request().Params.GetString("key") | ||
md5Secret := ctx.Request().Params.GetString("secret") | ||
if len(ownerKey) == 0 || len(md5Secret) == 0 { | ||
return api.ResponseWithCode(1, "require params key and secret") | ||
} | ||
if len(md5Secret) != 32 { | ||
return api.ResponseWithCode(2, "secret must be md5 crypte string") | ||
} | ||
cfg := gof.CurrentApp.Config() | ||
apiUser := cfg.GetString("api_user") | ||
apiSecret := cfg.GetString("api_secret") | ||
|
||
if ownerKey != "tmp_0606" { | ||
if apiUser != ownerKey || md5Secret != crypto.Md5([]byte(apiSecret)) { | ||
return api.ResponseWithCode(4, "用户或密钥不正确") | ||
} | ||
} | ||
// 创建token并返回 | ||
claims := api.CreateClaims("0", "go2o", | ||
"go2o-api-jwt", time.Now().Unix()+7200).(api.MapClaims) | ||
claims["global"] = true | ||
token, err := api.AccessToken(claims, getJWTSecret()) | ||
if err != nil { | ||
return api.ResponseWithCode(4, err.Error()) | ||
} | ||
return api.NewResponse(token) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.