diff options
Diffstat (limited to 'internal/acct/acct.go')
| -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 |