aboutsummaryrefslogtreecommitdiff
path: root/internal/auth/service.go
diff options
context:
space:
mode:
authorMax Resnick <max@ofmax.li>2020-11-08 11:45:16 -0800
committerMax Resnick <max@ofmax.li>2021-01-01 10:50:14 -0800
commita397341ad471cc761f7fb930d77e53cf7eb40a2a (patch)
tree76fb8318269569687fdd30467dc61ecba3499d09 /internal/auth/service.go
parent689a57ec4a444f8233fe2e5ec7ceb0903218218d (diff)
downloadiserv-a397341ad471cc761f7fb930d77e53cf7eb40a2a.tar.gz
adds casbin and accounts
Diffstat (limited to '')
-rw-r--r--internal/auth/service.go36
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
}