Skip to content

Commit

Permalink
feat: check chula email env
Browse files Browse the repository at this point in the history
  • Loading branch information
bookpanda committed Jul 7, 2024
1 parent 9893a20 commit 71ce02e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ JWT_ACCESS_TTL=3600
JWT_REFRESH_TTL=259200
JWT_ISSUER=issuer

AUTH_CHECK_CHULA_EMAIL=false

OAUTH_CLIENT_ID=client_id
OAUTH_CLIENT_SECRET=client_secret
OAUTH_REDIRECT_URI=http://localhost:3000
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func main() {
tokenSvc := token.NewService(jwtSvc, cacheRepo, token.NewTokenUtils(), logger.Named("tokenSvc"))
oauthConfig := config.LoadOauthConfig(conf.Oauth)
oauthClient := oauth.NewGoogleOauthClient(oauthConfig, logger.Named("oauthClient"))
authSvc := auth.NewService(oauthConfig, oauthClient, userSvc, tokenSvc, auth.NewAuthUtils(), logger.Named("authSvc"))
authSvc := auth.NewService(&conf.Auth, oauthConfig, oauthClient, userSvc, tokenSvc, auth.NewAuthUtils(), logger.Named("authSvc"))

listener, err := net.Listen("tcp", fmt.Sprintf(":%v", conf.App.Port))
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ type JwtConfig struct {
Issuer string
}

type AuthConfig struct {
CheckChulaEmail bool
}

type OauthConfig struct {
ClientId string
ClientSecret string
Expand All @@ -42,6 +46,7 @@ type Config struct {
Db DbConfig
Redis RedisConfig
Jwt JwtConfig
Auth AuthConfig
Oauth OauthConfig
}

Expand Down Expand Up @@ -89,6 +94,10 @@ func LoadConfig() (*Config, error) {
Issuer: os.Getenv("JWT_ISSUER"),
}

authConfig := AuthConfig{
CheckChulaEmail: os.Getenv("AUTH_CHECK_CHULA_EMAIL") == "true",
}

oauthConfig := OauthConfig{
ClientId: os.Getenv("OAUTH_CLIENT_ID"),
ClientSecret: os.Getenv("OAUTH_CLIENT_SECRET"),
Expand All @@ -100,6 +109,7 @@ func LoadConfig() (*Config, error) {
Db: dbConfig,
Redis: redisConfig,
Jwt: jwtConfig,
Auth: authConfig,
Oauth: oauthConfig,
}, nil
}
Expand Down
7 changes: 5 additions & 2 deletions internal/auth/auth.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/url"
"strings"

"github.com/isd-sgcu/rpkm67-auth/config"
"github.com/isd-sgcu/rpkm67-auth/internal/dto"
"github.com/isd-sgcu/rpkm67-auth/internal/oauth"
"github.com/isd-sgcu/rpkm67-auth/internal/token"
Expand All @@ -24,6 +25,7 @@ type Service interface {

type serviceImpl struct {
proto.UnimplementedAuthServiceServer
conf *config.AuthConfig
oauthConfig *oauth2.Config
oauthClient oauth.GoogleOauthClient
userSvc user.Service
Expand All @@ -32,8 +34,9 @@ type serviceImpl struct {
log *zap.Logger
}

func NewService(oauthConfig *oauth2.Config, oauthClient oauth.GoogleOauthClient, userSvc user.Service, tokenSvc token.Service, utils AuthUtils, log *zap.Logger) Service {
func NewService(conf *config.AuthConfig, oauthConfig *oauth2.Config, oauthClient oauth.GoogleOauthClient, userSvc user.Service, tokenSvc token.Service, utils AuthUtils, log *zap.Logger) Service {
return &serviceImpl{
conf: conf,
oauthConfig: oauthConfig,
oauthClient: oauthClient,
userSvc: userSvc,
Expand Down Expand Up @@ -104,7 +107,7 @@ func (s *serviceImpl) VerifyGoogleLogin(_ context.Context, in *proto.VerifyGoogl
}
}

if !IsEmailChulaStudent(email) {
if s.conf.CheckChulaEmail && !IsEmailChulaStudent(email) {
return nil, status.Error(codes.Unauthenticated, "Email is not a Chula student")
}

Expand Down

0 comments on commit 71ce02e

Please sign in to comment.