aboutsummaryrefslogtreecommitdiff
path: root/internal/auth/handler.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/auth/handler.go')
-rw-r--r--internal/auth/handler.go35
1 files changed, 15 insertions, 20 deletions
diff --git a/internal/auth/handler.go b/internal/auth/handler.go
index 992608c..d57d47e 100644
--- a/internal/auth/handler.go
+++ b/internal/auth/handler.go
@@ -3,15 +3,12 @@ package auth
import (
"encoding/json"
"io/ioutil"
- "log"
"net/http"
"github.com/alexedwards/scs/v2"
"golang.org/x/oauth2"
-)
-var (
- profileURL = "https://www.googleapis.com/oauth2/v3/userinfo"
+ "git.ofmax.li/iserv/internal/goog"
)
// Handler authentication handler
@@ -22,30 +19,28 @@ type Handler interface {
}
type authHandler struct {
- service Servicer
- ses *scs.SessionManager
- oclient *oauth2.Config
+ svc Servicer
+ ses *scs.SessionManager
}
+// TODO migrate to Goog
// NewHandler create new instance of handler
// Servicer, session, and oauth client required
func NewHandler(service Servicer,
- session *scs.SessionManager,
- oclient *oauth2.Config) Handler {
+ session *scs.SessionManager) Handler {
return &authHandler{
service,
session,
- oclient,
}
}
func (h *authHandler) Login(w http.ResponseWriter, r *http.Request) {
- stateValue, err := h.service.GenerateStateToken()
+ stateValue, err := h.svc.GenerateStateToken()
if err != nil {
return
}
h.ses.Put(r.Context(), "state", stateValue)
- url := h.oclient.AuthCodeURL(stateValue, oauth2.AccessTypeOnline)
+ url := h.svc.Goog().Config().AuthCodeURL(stateValue, oauth2.AccessTypeOnline)
http.Redirect(w, r, url, 302)
}
@@ -58,7 +53,7 @@ func (h *authHandler) OauthCallback(w http.ResponseWriter, r *http.Request) {
http.Error(w, "state value miss match bad data", 400)
return
}
- stateValid, err := h.service.ValidateStateToken(stateFromCallback, stateFromSession)
+ stateValid, err := h.svc.ValidateStateToken(stateFromCallback, stateFromSession)
if err != nil {
http.Error(w, "error validating", 400)
return
@@ -69,15 +64,14 @@ func (h *authHandler) OauthCallback(w http.ResponseWriter, r *http.Request) {
}
// valid and same as state
code := r.FormValue("code")
- token, err := h.oclient.Exchange(r.Context(), code)
+ token, err := h.svc.Goog().Config().Exchange(r.Context(), code)
if err != nil {
http.Error(w, err.Error(), 400)
return
}
- log.Printf("returned token %v", token)
// google profile
- client := h.oclient.Client(r.Context(), token)
- resp, err := client.Get(profileURL)
+ client := h.svc.Goog().UserClient(r.Context(), token)
+ resp, err := client.Get(goog.ProfileURL)
if err != nil {
http.Error(w, err.Error(), 400)
return
@@ -88,9 +82,9 @@ func (h *authHandler) OauthCallback(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), 400)
return
}
- gp := &GoogleAuthProfile{}
+ gp := &goog.GoogleProfile{}
err = json.Unmarshal(data, &gp)
- profileID, newProfile, err := h.service.LoginOrRegisterSessionID(token, gp)
+ profileID, newProfile, err := h.svc.LoginOrRegisterSessionID(token, gp)
if err != nil {
http.Error(w, err.Error(), 400)
return
@@ -98,8 +92,9 @@ func (h *authHandler) OauthCallback(w http.ResponseWriter, r *http.Request) {
h.ses.Put(r.Context(), "profid", profileID)
// send to registration
if newProfile == true {
- http.Redirect(w, r, "/account/register", 302)
+ http.Redirect(w, r, "/u/register", 302)
return
}
http.Redirect(w, r, "/", 302)
+ return
}