|
| 1 | +package auth |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "log/slog" |
| 7 | + |
| 8 | + "github.com/joshsoftware/code-curiosity-2025/internal/app/user" |
| 9 | + "github.com/joshsoftware/code-curiosity-2025/internal/config" |
| 10 | + "github.com/joshsoftware/code-curiosity-2025/internal/pkg/apperrors" |
| 11 | + "github.com/joshsoftware/code-curiosity-2025/internal/pkg/jwt" |
| 12 | + "github.com/joshsoftware/code-curiosity-2025/internal/pkg/middleware" |
| 13 | + "golang.org/x/oauth2" |
| 14 | + "golang.org/x/oauth2/github" |
| 15 | +) |
| 16 | + |
| 17 | +type service struct { |
| 18 | + githubOAuth2 oauth2.Config |
| 19 | + userService user.Service |
| 20 | + appCfg config.AppConfig |
| 21 | +} |
| 22 | + |
| 23 | +type Service interface { |
| 24 | + GithubOAuthLoginUrl(ctx context.Context) string |
| 25 | + GithubOAuthLoginCallback(ctx context.Context, code string) (string, error) |
| 26 | + GetLoggedInUser(ctx context.Context) (User, error) |
| 27 | +} |
| 28 | + |
| 29 | +func NewService(userService user.Service, appCfg config.AppConfig) Service { |
| 30 | + oauth2Config := oauth2.Config{ |
| 31 | + ClientID: appCfg.GithubOauth.ClientID, |
| 32 | + ClientSecret: appCfg.GithubOauth.ClientSecret, |
| 33 | + RedirectURL: appCfg.GithubOauth.RedirectURL, |
| 34 | + Endpoint: github.Endpoint, |
| 35 | + Scopes: []string{GithubOauthScope}, |
| 36 | + } |
| 37 | + |
| 38 | + return &service{ |
| 39 | + githubOAuth2: oauth2Config, |
| 40 | + userService: userService, |
| 41 | + appCfg: appCfg, |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func (s *service) GithubOAuthLoginUrl(ctx context.Context) string { |
| 46 | + return s.githubOAuth2.AuthCodeURL(GitHubOAuthState, oauth2.AccessTypeOffline) |
| 47 | +} |
| 48 | + |
| 49 | +func (s *service) GithubOAuthLoginCallback(ctx context.Context, code string) (string, error) { |
| 50 | + token, err := s.githubOAuth2.Exchange(ctx, code) |
| 51 | + if err != nil { |
| 52 | + slog.Error("failed to exchange token", "error", err) |
| 53 | + return "", apperrors.ErrGithubTokenExchangeFailed |
| 54 | + } |
| 55 | + |
| 56 | + client := s.githubOAuth2.Client(ctx, token) |
| 57 | + resp, err := client.Get(GetUserGithubUrl) |
| 58 | + if err != nil { |
| 59 | + slog.Error("failed to get user info", "error", err) |
| 60 | + return "", apperrors.ErrFailedToGetGithubUser |
| 61 | + } |
| 62 | + defer resp.Body.Close() |
| 63 | + |
| 64 | + var userInfo GithubUserResponse |
| 65 | + err = json.NewDecoder(resp.Body).Decode(&userInfo) |
| 66 | + if err != nil { |
| 67 | + slog.Error("failed to unmarshal user info", "error", err) |
| 68 | + return "", apperrors.ErrInternalServer |
| 69 | + } |
| 70 | + |
| 71 | + userData, err := s.userService.GetUserByGithubId(ctx, userInfo.GithubId) |
| 72 | + if err != nil { |
| 73 | + userData, err = s.userService.CreateUser(ctx, user.CreateUserRequestBody(userInfo)) |
| 74 | + if err != nil { |
| 75 | + slog.Error("failed to create user", "error", err) |
| 76 | + return "", apperrors.ErrUserCreationFailed |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + jwtToken, err := jwt.GenerateJWT(userData.Id, userInfo.IsAdmin, s.appCfg) |
| 81 | + if err != nil { |
| 82 | + slog.Error("error generating jwt", "error", err) |
| 83 | + return "", apperrors.ErrInternalServer |
| 84 | + } |
| 85 | + |
| 86 | + return jwtToken, nil |
| 87 | +} |
| 88 | + |
| 89 | +func (s *service) GetLoggedInUser(ctx context.Context) (User, error) { |
| 90 | + userIdValue := ctx.Value(middleware.UserIdKey) |
| 91 | + |
| 92 | + userId, ok := userIdValue.(int) |
| 93 | + if !ok { |
| 94 | + slog.Error("error obtaining user id from context") |
| 95 | + return User{}, apperrors.ErrInternalServer |
| 96 | + } |
| 97 | + |
| 98 | + user, err := s.userService.GetUserById(ctx, userId) |
| 99 | + if err != nil { |
| 100 | + slog.Error("failed to get logged in user", "error", err) |
| 101 | + return User{}, apperrors.ErrInternalServer |
| 102 | + } |
| 103 | + |
| 104 | + return User(user), nil |
| 105 | +} |
0 commit comments