update api.go, auth.go, docs.go, handler.go, service.go, swagger.json, swagger.yaml
This commit is contained in:
1 parent
4c8b4e9fce
commit
362a6d1041
7 files changed
+1564
No files matched your search
@@ -0,0 +1,77 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"esauth"
|
||||
"esauth/pkg/repository"
|
||||
"math"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/dgrijalva/jwt-go"
|
||||
)
|
||||
|
||||
const (
|
||||
tokenTTL = 12 * time.Hour
|
||||
)
|
||||
|
||||
type tokenClaims struct {
|
||||
jwt.StandardClaims
|
||||
UserId int64 `json:"user_id"`
|
||||
}
|
||||
|
||||
type AuthService struct {
|
||||
repo *repository.Repository
|
||||
}
|
||||
|
||||
func NewAuthService(repo *repository.Repository) *AuthService {
|
||||
return &AuthService{repo: repo}
|
||||
}
|
||||
|
||||
func (s *AuthService) CreateUser(user *esauth.User) (int64, error) {
|
||||
return s.repo.CreateUser(user)
|
||||
}
|
||||
func (s *AuthService) SaveUser(user *esauth.User) (int64, error) {
|
||||
return s.repo.SaveUser(user)
|
||||
}
|
||||
func (s *AuthService) GenerateToken(userId int64) (string, error) {
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, &tokenClaims{
|
||||
jwt.StandardClaims{
|
||||
ExpiresAt: time.Now().Add(tokenTTL).Unix(),
|
||||
IssuedAt: time.Now().Unix(),
|
||||
},
|
||||
userId,
|
||||
})
|
||||
return token.SignedString([]byte(os.Getenv("SIGN_KEY")))
|
||||
}
|
||||
|
||||
func (s *AuthService) GenerateInfinityToken(userId int64) (string, error) {
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, &tokenClaims{
|
||||
jwt.StandardClaims{
|
||||
ExpiresAt: math.MaxInt64,
|
||||
IssuedAt: time.Now().Unix(),
|
||||
},
|
||||
userId,
|
||||
})
|
||||
return token.SignedString([]byte(os.Getenv("SIGN_KEY")))
|
||||
}
|
||||
|
||||
func (s *AuthService) ParseToken(accessToken string) (int64, error) {
|
||||
token, err := jwt.ParseWithClaims(accessToken, &tokenClaims{}, func(token *jwt.Token) (interface{}, error) {
|
||||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, errors.New("invalid signing method")
|
||||
}
|
||||
|
||||
return []byte(os.Getenv("SIGN_KEY")), nil
|
||||
})
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
claims, ok := token.Claims.(*tokenClaims)
|
||||
if !ok {
|
||||
return 0, errors.New("token claims are not of type *tokenClaims")
|
||||
}
|
||||
|
||||
return claims.UserId, nil
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"esauth"
|
||||
"esauth/pkg/repository"
|
||||
)
|
||||
|
||||
type Authorization interface {
|
||||
CreateUser(user *esauth.User) (int64, error)
|
||||
SaveUser(user *esauth.User) (int64, error)
|
||||
GenerateToken(userId int64) (string, error)
|
||||
GenerateInfinityToken(userId int64) (string, error)
|
||||
ParseToken(token string) (int64, error)
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
Authorization
|
||||
}
|
||||
|
||||
func NewService(repos *repository.Repository) *Service {
|
||||
return &Service{
|
||||
Authorization: NewAuthService(repos),
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user