diff options
| author | Max Resnick <max@ofmax.li> | 2020-11-08 11:45:16 -0800 |
|---|---|---|
| committer | Max Resnick <max@ofmax.li> | 2021-01-01 10:50:14 -0800 |
| commit | a397341ad471cc761f7fb930d77e53cf7eb40a2a (patch) | |
| tree | 76fb8318269569687fdd30467dc61ecba3499d09 /internal/acct/acct.go | |
| parent | 689a57ec4a444f8233fe2e5ec7ceb0903218218d (diff) | |
| download | iserv-a397341ad471cc761f7fb930d77e53cf7eb40a2a.tar.gz | |
adds casbin and accounts
Diffstat (limited to '')
| -rw-r--r-- | internal/acct/acct.go | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/internal/acct/acct.go b/internal/acct/acct.go new file mode 100644 index 0000000..48a71e6 --- /dev/null +++ b/internal/acct/acct.go @@ -0,0 +1,106 @@ +package acct + +import ( + "context" + "net/http" + + "git.ofmax.li/iserv/internal/auth" + "git.ofmax.li/iserv/internal/goog" + "github.com/alexedwards/scs/v2" + "golang.org/x/oauth2" +) + +// handler + +// Handler interface to http handler +type Handler interface { + Register(w http.ResponseWriter, r *http.Request) +} + +type acctHandler struct { + svc Servicer + ses *scs.SessionManager +} + +func (h *acctHandler) Register(w http.ResponseWriter, r *http.Request) { + userID := h.ses.GetString(r.Context(), "profid") + if userID == "" { + http.Redirect(w, r, "/l", http.StatusFound) + } + h.svc.GetGoogProfile(r.Context(), userID) + return +} + +// NewHandler create new instance of handler +// Servicer, session, and oauth client required +func NewHandler(service Servicer, session *scs.SessionManager) Handler { + return &acctHandler{ + service, + session, + } +} + +// endhandler + +// model + +// Profile application account profile +type Profile interface { + ProfileKeyValues() (string, []string) +} + +// endmodel + +// repo +// Repo storage interface + +type Repo interface { + UpdateAcctProfile(p Profile) error +} + +// endrepo + +// service + +type Servicer interface { + UpdateProfile(p *Profile) error + GetGoogProfile(ctx context.Context, id string) error +} + +func NewService(repo Repo, authRepo auth.Repo, goog goog.Servicer) *service { + return &service{ + repo, + authRepo, + goog, + } +} + +type service struct { + repo Repo + authRepo auth.Repo + goog goog.Servicer +} + +func (s *service) UpdateProfile(p *Profile) error { + return nil +} + +func (s *service) GetGoogProfile(ctx context.Context, id string) error { + token, err := s.authRepo.GetProfileToken(id) + if err != nil { + return err + } + client := s.goog.UserClient(ctx, token) + gp, _, err := s.goog.Profile(client) + if err != nil { + return err + } + s.repo.UpdateAcctProfile(gp) + return nil +} + +func (s *service) GetProfileToken(id string) (*oauth2.Token, error) { + return s.authRepo.GetProfileToken(id) +} + +// endservice |