Skip to content

Commit

Permalink
fix: auth issues
Browse files Browse the repository at this point in the history
  • Loading branch information
crlssn committed Nov 18, 2024
1 parent 3907989 commit 34113b2
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 37 deletions.
8 changes: 8 additions & 0 deletions .github/actions/deploy/backend/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ inputs:
cookie_domain:
description: "Cookie Domain"
required: true
jwt_access_token_key:
description: "JWT Access Token Key"
required: true
jwt_refresh_token_key:
description: "JWT Refresh Token
required: true
runs:
using: 'composite'
Expand Down Expand Up @@ -77,6 +83,8 @@ runs:
echo SERVER_CERT_PATH=${{ inputs.server_cert_path }} >> .env
echo SERVER_KEY_PATH=${{ inputs.server_key_path }} >> .env
echo COOKIE_DOMAIN=${{ inputs.cookie_domain }} >> .env
echo JWT_ACCESS_TOKEN_KEY=${{ inputs.jwt_access_token_key }} >> .env
echo JWT_REFRESH_TOKEN_KEY=${{ inputs.jwt_refresh_token_key }} >> .env
- name: Deploy to EC2 instance
uses: easingthemes/[email protected]
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ jobs:
server_cert_path: ${{ vars.SERVER_CERT_PATH }}
server_key_path: ${{ vars.SERVER_KEY_PATH }}
cookie_domain: ${{ vars.COOKIE_DOMAIN }}
jwt_access_token_key: ${{ secrets.JWT_ACCESS_TOKEN_KEY }}
jwt_refresh_token_key: ${{ secrets.JWT_REFRESH_TOKEN_KEY }}

- name: Deploy web to S3
uses: ./.github/actions/deploy/web
Expand Down
1 change: 1 addition & 0 deletions apps/backend/pkg/jwt/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func (m *Manager) CreateToken(userID string, tokenType TokenType) (string, error
if err != nil {
return "", fmt.Errorf("signing token: %w", err)
}

return tokenString, nil
}

Expand Down
8 changes: 6 additions & 2 deletions apps/backend/pkg/jwt/module.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package jwt

import "go.uber.org/fx"
import (
"os"

"go.uber.org/fx"
)

func Module() fx.Option {
return fx.Provide(
func() *Manager {
return NewManager([]byte("access-key"), []byte("refresh-key"))
return NewManager([]byte(os.Getenv("JWT_ACCESS_TOKEN_KEY")), []byte(os.Getenv("JWT_REFRESH_TOKEN_KEY")))
},
)
}
2 changes: 0 additions & 2 deletions apps/backend/pkg/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,10 @@ func (r *Repo) ListExercises(ctx context.Context, p ListExercisesParams) (orm.Ex
query = append(query, orm.ExerciseWhere.CreatedAt.LT(pt.CreatedAt))
}

boil.DebugMode = true
exercises, err := orm.Exercises(query...).All(ctx, r.executor())
if err != nil {
return nil, nil, fmt.Errorf("exercises fetch: %w", err)
}
boil.DebugMode = false

if len(exercises) > p.Limit {
pt, ptErr := json.Marshal(pageToken{CreatedAt: exercises[p.Limit-1].CreatedAt})
Expand Down
12 changes: 8 additions & 4 deletions apps/backend/rpc/interceptors/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/crlssn/getstronger/apps/backend/pkg/jwt"
v1 "github.com/crlssn/getstronger/apps/backend/pkg/pb/api/v1"
"github.com/crlssn/getstronger/apps/backend/pkg/xzap"
)

type auth struct {
Expand Down Expand Up @@ -72,27 +73,30 @@ func (a *auth) initMethods() {

// Unary is the unary interceptor method for authentication.
func (a *auth) Unary() connect.UnaryInterceptorFunc {
interceptor := func(next connect.UnaryFunc) connect.UnaryFunc {
return func(next connect.UnaryFunc) connect.UnaryFunc {
return func(
ctx context.Context,
req connect.AnyRequest,
) (connect.AnyResponse, error) {
log := a.log.With(xzap.FieldRPC(req.Spec().Procedure))
log.Info("request received")

requiresAuth := a.methods[req.Spec().Procedure]
if !requiresAuth {
a.log.Info("method does not require authentication", zap.String("method", req.Spec().Procedure))
log.Info("request does not require authentication")
return next(ctx, req)
}

claims, err := a.claimsFromHeader(req.Header())
if err != nil {
a.log.Warn("unauthenticated request", zap.Error(err))
log.Warn("request unauthenticated", zap.Error(err))
return nil, connect.NewError(connect.CodeUnauthenticated, nil)
}

log.Info("request authenticated", xzap.FieldUserID(claims.UserID), zap.Any("claims", claims))
return next(context.WithValue(ctx, jwt.ContextKeyUserID, claims.UserID), req)
}
}
return interceptor
}

var (
Expand Down
45 changes: 16 additions & 29 deletions apps/backend/rpc/v1/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net/http"
"os"
"strings"
"time"

"connectrpc.com/connect"
"go.uber.org/zap"
Expand Down Expand Up @@ -114,20 +113,14 @@ func (h *auth) Login(ctx context.Context, req *connect.Request[v1.LoginRequest])
})

cookie := &http.Cookie{
Name: "refreshToken",
Value: refreshToken,
Quoted: false,
Path: "/api.v1.AuthService",
Domain: os.Getenv("COOKIE_DOMAIN"),
Expires: time.Time{},
RawExpires: "",
MaxAge: int(jwt.ExpiryTimeRefresh),
Secure: true,
HttpOnly: true,
SameSite: http.SameSiteNoneMode,
Partitioned: false,
Raw: "",
Unparsed: nil,
Name: "refreshToken",
Value: refreshToken,
Path: "/api.v1.AuthService",
Domain: os.Getenv("COOKIE_DOMAIN"),
MaxAge: int(jwt.ExpiryTimeRefresh),
Secure: true,
HttpOnly: true,
SameSite: http.SameSiteNoneMode,
}
res.Header().Set("Set-Cookie", cookie.String())

Expand Down Expand Up @@ -195,20 +188,14 @@ func (h *auth) Logout(ctx context.Context, _ *connect.Request[v1.LogoutRequest])

res := connect.NewResponse(&v1.LogoutResponse{})
cookie := &http.Cookie{
Name: "refreshToken",
Value: "",
Quoted: false,
Path: "/api.v1.AuthService",
Domain: os.Getenv("COOKIE_DOMAIN"),
Expires: time.Time{},
RawExpires: "",
MaxAge: -1,
Secure: true,
HttpOnly: true,
SameSite: http.SameSiteNoneMode,
Partitioned: false,
Raw: "",
Unparsed: nil,
Name: "refreshToken",
Value: "",
Path: "/api.v1.AuthService",
Domain: os.Getenv("COOKIE_DOMAIN"),
MaxAge: -1,
Secure: true,
HttpOnly: true,
SameSite: http.SameSiteNoneMode,
}
res.Header().Set("Set-Cookie", cookie.String())

Expand Down

0 comments on commit 34113b2

Please sign in to comment.