-
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
13 changed files
with
150 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package api | ||
|
||
import ( | ||
"auth-server/middleware" | ||
"auth-server/model" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
type AuthApi struct{} | ||
|
||
func (AuthApi) Login(c *gin.Context) { | ||
var param model.LoginUser | ||
if err := c.ShouldBindJSON(¶m); err != nil { | ||
middleware.Fail(c, model.ErrParam.AddErr(err)) | ||
return | ||
} | ||
token, err := authService.Login(param) | ||
middleware.Auto(c, err, map[string]string{ | ||
"token": token, | ||
}) | ||
return | ||
} | ||
|
||
func (AuthApi) Register(c *gin.Context) { | ||
var param model.User | ||
if err := c.ShouldBindJSON(¶m); err != nil { | ||
middleware.Fail(c, model.ErrParam.AddErr(err)) | ||
return | ||
} | ||
err := authService.Register(¶m) | ||
middleware.Auto(c, err, nil) | ||
return | ||
} |
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 |
---|---|---|
|
@@ -6,4 +6,5 @@ import ( | |
|
||
var ( | ||
userService services.UserService | ||
authService services.AuthService | ||
) |
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
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
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
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,8 @@ | ||
package router | ||
|
||
import "github.com/gin-gonic/gin" | ||
|
||
func authRouter(router *gin.RouterGroup) { | ||
router.POST("/auth/login", apiAuth.Login) | ||
router.POST("/auth/register", apiAuth.Register) | ||
} |
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
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,61 @@ | ||
package services | ||
|
||
import ( | ||
"auth-server/common" | ||
"auth-server/config" | ||
"auth-server/model" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
type AuthService struct{} | ||
|
||
var ( | ||
UserNotFoundErr = errors.New("user not found") | ||
UserExistErr = errors.New("user exist") | ||
) | ||
|
||
func (AuthService) Register(user *model.User) (err model.ErrorCode) { | ||
var found bool | ||
_, found, err = proxyUser.GetUserByUsername(user.Username) | ||
if err.Code != 0 { | ||
return | ||
} | ||
if found { | ||
err = model.ErrRegisterParam.AddErr(UserExistErr) | ||
return | ||
} | ||
|
||
// 产生slat | ||
slat := common.GetRandomString(16) | ||
user.Salt = slat | ||
// todo 后续采用加密更强的算法,例如AES | ||
user.Password = common.MD5(slat + user.Password) | ||
err = proxyUser.CreateUser(user) | ||
return | ||
} | ||
|
||
func (AuthService) Login(loginUser model.LoginUser) (token string, err model.ErrorCode) { | ||
var user model.User | ||
var found bool | ||
user, found, err = proxyUser.GetUserByUsername(loginUser.Username) | ||
if err.Code != 0 { | ||
return | ||
} | ||
if !found { | ||
err = model.ErrLonginParam.AddErr(UserNotFoundErr) | ||
return | ||
} | ||
slat := user.Salt | ||
enPassword := common.MD5(slat + loginUser.Password) | ||
if enPassword != user.Password { | ||
err = model.ErrLonginParam | ||
return | ||
} | ||
var genTokenErr error | ||
token, genTokenErr = common.GenerateToken(loginUser.Username, config.JwtSecret, config.JwtExpire, config.ServerName) | ||
if genTokenErr != nil { | ||
err = model.ErrGenToken.AddErr(genTokenErr) | ||
return | ||
} | ||
return | ||
} |
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 |
---|---|---|
@@ -1,22 +1,12 @@ | ||
package services | ||
|
||
import ( | ||
"auth-server/common" | ||
"auth-server/model" | ||
) | ||
|
||
type UserService struct{} | ||
|
||
func (UserService) CreateUser(user *model.User) (err model.ErrorCode) { | ||
// 产生slat | ||
slat := common.GetRandomString(16) | ||
// todo 后续采用加密更强的算法,例如AES | ||
user.Password = common.MD5(slat) | ||
err = proxyUser.CreateUser(user) | ||
return | ||
} | ||
|
||
func (UserService) GetUser(userId int64) (user *model.User, err model.ErrorCode) { | ||
user, err = proxyUser.GetUser(userId) | ||
func (UserService) GetUser(userId int64) (user model.User, err model.ErrorCode) { | ||
user, _, err = proxyUser.GetUserById(userId) | ||
return | ||
} |