feat: ci/cd
This commit is contained in:
@@ -41,38 +41,6 @@ func (s *Service) Register(ctx context.Context, input models.UserRegisterInput)
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Service) RefreshTokens(ctx context.Context, rawToken string) (*TokenPair, error) {
|
||||
row, err := s.oauth.GetRefreshTokenByHash(ctx, sha256Hex(rawToken))
|
||||
if err != nil {
|
||||
return nil, models.ErrUnauthorized
|
||||
}
|
||||
|
||||
if time.Now().After(row.ExpiresAt) {
|
||||
return nil, models.ErrUnauthorized
|
||||
}
|
||||
|
||||
client, err := s.oauth.GetOAuthClientByID(ctx, row.ClientID)
|
||||
if err != nil {
|
||||
return nil, models.ErrInternal
|
||||
}
|
||||
|
||||
scopes := scopesFromJSON(row.Scopes)
|
||||
if !ScopesAllowed(client.Scopes, scopes) {
|
||||
return nil, ErrInvalidGrant
|
||||
}
|
||||
|
||||
if err := s.oauth.DeleteRefreshTokenByID(ctx, row.ID); err != nil {
|
||||
return nil, models.ErrInternal
|
||||
}
|
||||
|
||||
user, err := s.users.GetUserByID(ctx, row.UserID)
|
||||
if err != nil {
|
||||
return nil, models.ErrUnauthorized
|
||||
}
|
||||
|
||||
return s.issuePair(ctx, user, client.ID, scopes)
|
||||
}
|
||||
|
||||
func (s *Service) ValidateAccessToken(ctx context.Context, token string) (*TokenInfo, error) {
|
||||
token = strings.TrimSpace(token)
|
||||
if token == "" {
|
||||
@@ -105,15 +73,19 @@ func (s *Service) UpdateProfile(ctx context.Context, userID uuid.UUID, input mod
|
||||
if input.FirstName != "" {
|
||||
user.FirstName = input.FirstName
|
||||
}
|
||||
|
||||
if input.SecondName != "" {
|
||||
user.SecondName = input.SecondName
|
||||
}
|
||||
|
||||
if input.BirthDate != nil {
|
||||
user.BirthDate = input.BirthDate
|
||||
}
|
||||
|
||||
if input.Sex != "" {
|
||||
user.Sex = input.Sex
|
||||
}
|
||||
|
||||
if input.Password != "" {
|
||||
hash, err := bcrypt.GenerateFromPassword([]byte(input.Password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
@@ -121,15 +93,22 @@ func (s *Service) UpdateProfile(ctx context.Context, userID uuid.UUID, input mod
|
||||
}
|
||||
user.PasswordHash = string(hash)
|
||||
}
|
||||
|
||||
user.UpdatedAt = time.Now()
|
||||
|
||||
return s.users.UpdateUser(ctx, *user)
|
||||
}
|
||||
|
||||
func (s *Service) DeleteProfile(ctx context.Context, userID uuid.UUID, accessToken string) error {
|
||||
if err := s.oauth.DeleteRefreshTokensByUserID(ctx, userID); err != nil {
|
||||
return models.ErrInternal
|
||||
}
|
||||
|
||||
if err := s.users.DeleteUser(ctx, userID); err != nil {
|
||||
return models.ErrInternal
|
||||
}
|
||||
|
||||
_ = s.redis.Del(ctx, accessTokenPrefix+accessToken)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -96,12 +96,12 @@ func (s *Service) saveSession(ctx context.Context, token string, sess *OAuthSess
|
||||
func (s *Service) SessionLogin(ctx context.Context, token, email, password string) (string, error) {
|
||||
sess, err := s.GetOAuthSession(ctx, token)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return "", ErrInvalidGrant
|
||||
}
|
||||
|
||||
user, err := s.users.GetUserByEmail(ctx, email)
|
||||
if err != nil || bcryptCompare(user.PasswordHash, password) != nil {
|
||||
return "", models.ErrUnauthorized
|
||||
return "", ErrAccessDenied
|
||||
}
|
||||
|
||||
sess.UserID = user.ID
|
||||
@@ -114,12 +114,7 @@ func (s *Service) SessionLogin(ctx context.Context, token, email, password strin
|
||||
return "", ErrInvalidClient
|
||||
}
|
||||
|
||||
if client.Trusted {
|
||||
return s.SessionApprove(ctx, token)
|
||||
}
|
||||
|
||||
hasUserConsents := s.HasUserConsent(ctx, sess.UserID, client.ID, sess.Scopes)
|
||||
if hasUserConsents {
|
||||
if client.Trusted || s.HasUserConsent(ctx, sess.UserID, client.ID, sess.Scopes) {
|
||||
return s.SessionApprove(ctx, token)
|
||||
}
|
||||
|
||||
@@ -129,12 +124,12 @@ func (s *Service) SessionLogin(ctx context.Context, token, email, password strin
|
||||
func (s *Service) SessionAttach(ctx context.Context, sessionToken, accessToken string) (string, error) {
|
||||
info, err := s.ValidateAccessToken(ctx, accessToken)
|
||||
if err != nil {
|
||||
return "", models.ErrUnauthorized
|
||||
return "", ErrAccessDenied
|
||||
}
|
||||
|
||||
sess, err := s.GetOAuthSession(ctx, sessionToken)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return "", ErrInvalidGrant
|
||||
}
|
||||
|
||||
sess.UserID = info.UserID
|
||||
@@ -146,12 +141,8 @@ func (s *Service) SessionAttach(ctx context.Context, sessionToken, accessToken s
|
||||
if err != nil {
|
||||
return "", ErrInvalidClient
|
||||
}
|
||||
if client.Trusted {
|
||||
return s.SessionApprove(ctx, sessionToken)
|
||||
}
|
||||
|
||||
hasUserConsents := s.HasUserConsent(ctx, sess.UserID, client.ID, sess.Scopes)
|
||||
if hasUserConsents {
|
||||
if client.Trusted || s.HasUserConsent(ctx, sess.UserID, client.ID, sess.Scopes) {
|
||||
return s.SessionApprove(ctx, sessionToken)
|
||||
}
|
||||
|
||||
@@ -161,7 +152,7 @@ func (s *Service) SessionAttach(ctx context.Context, sessionToken, accessToken s
|
||||
func (s *Service) SessionApprove(ctx context.Context, token string) (string, error) {
|
||||
sess, err := s.GetOAuthSession(ctx, token)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return "", ErrInvalidGrant
|
||||
}
|
||||
if sess.UserID == uuid.Nil {
|
||||
return "", ErrInvalidGrant
|
||||
@@ -178,7 +169,7 @@ func (s *Service) SessionApprove(ctx context.Context, token string) (string, err
|
||||
}
|
||||
|
||||
_ = s.redis.Del(ctx, oauthSessionPrefix+token)
|
||||
s.oauth.RecordUserConsent(ctx, sess.UserID, client.ID, sess.Scopes)
|
||||
_ = s.oauth.RecordUserConsent(ctx, sess.UserID, client.ID, sess.Scopes)
|
||||
|
||||
redirectURL, _ := url.Parse(sess.RedirectURI)
|
||||
q := redirectURL.Query()
|
||||
@@ -221,7 +212,7 @@ func (s *Service) ExchangeCode(ctx context.Context, auth ClientAuth, code, redir
|
||||
return nil, models.ErrInternal
|
||||
}
|
||||
|
||||
s.oauth.UpdateConsentLastUsed(ctx, row.UserID, client.ID)
|
||||
_ = s.oauth.UpdateConsentLastUsed(ctx, row.UserID, client.ID)
|
||||
|
||||
return s.issuePair(ctx, user, client.ID, scopesFromJSON(row.Scopes))
|
||||
}
|
||||
@@ -241,22 +232,22 @@ func (s *Service) ExchangeRefreshToken(ctx context.Context, auth ClientAuth, raw
|
||||
return nil, ErrInvalidGrant
|
||||
}
|
||||
|
||||
scopes := scopesFromJSON(row.Scopes)
|
||||
if !ScopesAllowed(client.Scopes, scopes) {
|
||||
return nil, ErrInvalidGrant
|
||||
}
|
||||
|
||||
if err := s.oauth.DeleteRefreshTokenByID(ctx, row.ID); err != nil {
|
||||
return nil, models.ErrInternal
|
||||
}
|
||||
|
||||
s.oauth.UpdateConsentLastUsed(ctx, row.UserID, client.ID)
|
||||
_ = s.oauth.UpdateConsentLastUsed(ctx, row.UserID, client.ID)
|
||||
|
||||
user, err := s.users.GetUserByID(ctx, row.UserID)
|
||||
if err != nil {
|
||||
return nil, models.ErrInternal
|
||||
}
|
||||
|
||||
scopes := scopesFromJSON(row.Scopes)
|
||||
if !ScopesAllowed(client.Scopes, scopes) {
|
||||
return nil, ErrInvalidGrant
|
||||
}
|
||||
|
||||
return s.issuePair(ctx, user, client.ID, scopes)
|
||||
}
|
||||
|
||||
@@ -275,13 +266,7 @@ func (s *Service) RevokeToken(ctx context.Context, auth ClientAuth, rawToken str
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) createAuthCode(
|
||||
ctx context.Context,
|
||||
userID, clientID uuid.UUID,
|
||||
redirectURI string,
|
||||
scopes []string,
|
||||
codeChallenge string,
|
||||
) (string, error) {
|
||||
func (s *Service) createAuthCode(ctx context.Context, userID, clientID uuid.UUID, redirectURI string, scopes []string, codeChallenge string) (string, error) {
|
||||
rawCode, err := newOpaqueToken(32)
|
||||
if err != nil {
|
||||
return "", models.ErrInternal
|
||||
@@ -330,6 +315,7 @@ func (s *Service) issuePair(ctx context.Context, user *models.User, clientID uui
|
||||
Scopes: scopes,
|
||||
ClientID: clientID,
|
||||
}
|
||||
|
||||
data, _ := json.Marshal(accessData)
|
||||
if err := s.redis.Set(ctx, accessTokenPrefix+accessToken, data, s.tokens.AccessTTL).Err(); err != nil {
|
||||
return nil, models.ErrInternal
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
type RateLimitConfig struct {
|
||||
Max int
|
||||
Window time.Duration
|
||||
BlockFor time.Duration
|
||||
}
|
||||
|
||||
var (
|
||||
LoginRateLimit = RateLimitConfig{
|
||||
Max: 10,
|
||||
Window: 15 * time.Minute,
|
||||
BlockFor: 15 * time.Minute,
|
||||
}
|
||||
|
||||
RegisterRateLimit = RateLimitConfig{
|
||||
Max: 5,
|
||||
Window: time.Hour,
|
||||
}
|
||||
)
|
||||
|
||||
type RateLimiter struct {
|
||||
redis *redis.Client
|
||||
}
|
||||
|
||||
func NewRateLimiter(rdb *redis.Client) *RateLimiter {
|
||||
return &RateLimiter{redis: rdb}
|
||||
}
|
||||
|
||||
func (r *RateLimiter) Allow(ctx context.Context, key string, cfg RateLimitConfig) (bool, int, error) {
|
||||
blockKey := fmt.Sprintf("rl_block:%s", key)
|
||||
counterKey := fmt.Sprintf("rl_count:%s", key)
|
||||
|
||||
if cfg.BlockFor > 0 {
|
||||
blocked, err := r.redis.Exists(ctx, blockKey).Result()
|
||||
if err != nil {
|
||||
return true, 0, nil
|
||||
}
|
||||
if blocked > 0 {
|
||||
return false, 0, nil
|
||||
}
|
||||
}
|
||||
|
||||
pipe := r.redis.Pipeline()
|
||||
incrCmd := pipe.Incr(ctx, counterKey)
|
||||
pipe.Expire(ctx, counterKey, cfg.Window)
|
||||
if _, err := pipe.Exec(ctx); err != nil {
|
||||
return true, 0, nil
|
||||
}
|
||||
|
||||
count := int(incrCmd.Val())
|
||||
remaining := cfg.Max - count
|
||||
if remaining < 0 {
|
||||
remaining = 0
|
||||
}
|
||||
|
||||
if count > cfg.Max {
|
||||
if cfg.BlockFor > 0 {
|
||||
_ = r.redis.Set(ctx, blockKey, 1, cfg.BlockFor).Err()
|
||||
}
|
||||
return false, 0, nil
|
||||
}
|
||||
|
||||
return true, remaining, nil
|
||||
}
|
||||
|
||||
func (r *RateLimiter) Reset(ctx context.Context, key string) {
|
||||
_ = r.redis.Del(ctx, fmt.Sprintf("rl_count:%s", key), fmt.Sprintf("rl_block:%s", key))
|
||||
}
|
||||
@@ -37,6 +37,7 @@ type OAuthRepo interface {
|
||||
CreateRefreshToken(ctx context.Context, row models.RefreshTokenRow) (*models.RefreshTokenRow, error)
|
||||
GetRefreshTokenByHash(ctx context.Context, tokenHash string) (*models.RefreshTokenRow, error)
|
||||
DeleteRefreshTokenByID(ctx context.Context, id uuid.UUID) error
|
||||
DeleteRefreshTokensByUserID(ctx context.Context, userID uuid.UUID) error
|
||||
RecordUserConsent(ctx context.Context, userID, clientID uuid.UUID, scopes []string) error
|
||||
GetUserConsents(ctx context.Context, userID uuid.UUID) ([]models.UserConsent, error)
|
||||
UpdateConsentLastUsed(ctx context.Context, userID, clientID uuid.UUID) error
|
||||
@@ -55,11 +56,10 @@ type ClientAuth struct {
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
users UserRepo
|
||||
oauth OAuthRepo
|
||||
redis *redis.Client
|
||||
tokens TokenConfig
|
||||
basicClient *models.OAuthClient
|
||||
users UserRepo
|
||||
oauth OAuthRepo
|
||||
redis *redis.Client
|
||||
tokens TokenConfig
|
||||
}
|
||||
|
||||
func New(
|
||||
@@ -67,14 +67,12 @@ func New(
|
||||
oauth OAuthRepo,
|
||||
rdb *redis.Client,
|
||||
tokenCfg TokenConfig,
|
||||
basicClient *models.OAuthClient,
|
||||
) *Service {
|
||||
return &Service{
|
||||
users: users,
|
||||
oauth: oauth,
|
||||
redis: rdb,
|
||||
tokens: tokenCfg,
|
||||
basicClient: basicClient,
|
||||
users: users,
|
||||
oauth: oauth,
|
||||
redis: rdb,
|
||||
tokens: tokenCfg,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user