diff options
Diffstat (limited to 'internal/auth/service.go')
| -rw-r--r-- | internal/auth/service.go | 36 |
1 files changed, 30 insertions, 6 deletions
diff --git a/internal/auth/service.go b/internal/auth/service.go index 9997264..e85c705 100644 --- a/internal/auth/service.go +++ b/internal/auth/service.go @@ -2,11 +2,14 @@ package auth import ( "errors" + "fmt" "log" "time" + "git.ofmax.li/iserv/internal/goog" "golang.org/x/oauth2" + "github.com/casbin/casbin" "github.com/gbrlsnchs/jwt/v3" ) @@ -22,23 +25,40 @@ var ( // Servicer access to auth functionality type Servicer interface { - LoginOrRegisterSessionID(t *oauth2.Token, gp *GoogleAuthProfile) (string, bool, error) + Goog() goog.Servicer + LoginOrRegisterSessionID(t *oauth2.Token, gp *goog.GoogleProfile) (string, bool, error) GenerateStateToken() (string, error) ValidateStateToken(token string, sessionToken string) (bool, error) + CheckProfileID(id string) (bool, error) + Enf() *casbin.Enforcer } // Service a container for auth deps type Service struct { repo Repo + goog goog.Servicer + enf *casbin.Enforcer } // NewService create auth service -func NewService(repo Repo) *Service { +func NewService(repo Repo, goog goog.Servicer, enf *casbin.Enforcer) *Service { return &Service{ repo, + goog, + enf, } } +// Goog get google interface +func (a *Service) Goog() goog.Servicer { + return a.goog +} + +// Enf enforcer instance +func (a *Service) Enf() *casbin.Enforcer { + return a.enf +} + // GenerateStateToken create a random token for oauth exchange func (a *Service) GenerateStateToken() (string, error) { now := time.Now() @@ -67,8 +87,13 @@ func (a *Service) ValidateStateToken(token string, sessionToken string) (bool, e return false, ErrInvalidToken } +// CheckProfileID check if a profileid exists +func (a *Service) CheckProfileID(id string) (bool, error) { + return a.repo.CheckProfileID(id) +} + // LoginOrRegisterSessionID create a login -func (a *Service) LoginOrRegisterSessionID(t *oauth2.Token, gp *GoogleAuthProfile) (string, bool, error) { +func (a *Service) LoginOrRegisterSessionID(t *oauth2.Token, gp *goog.GoogleProfile) (string, bool, error) { isAuthorized, err := a.repo.IsAuthorized(gp) newRegistration := false if err != nil { @@ -84,10 +109,9 @@ func (a *Service) LoginOrRegisterSessionID(t *oauth2.Token, gp *GoogleAuthProfil if profileID == "" { // create profile log.Printf("creating new profile") - profile := NewAuthProfile(t, gp) + profile := NewAuthProfile(t, fmt.Sprintf("goog:%s", gp.ProfileID)) profileID = profile.ID - log.Printf("new profile %+v", profile) - err = a.repo.SaveAuthProfile(profile) + err = a.repo.SaveAuthProfile(gp.Email, profile) if err != nil { return "", newRegistration, err } |