save oauth user_id and link users and accounts
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
package site
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// POST /accounts
|
||||
func (s *Server) createAccount(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
email := r.FormValue("email")
|
||||
if email == "" {
|
||||
http.Error(w, "no email provided", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
userID := getAccessTokenClaims(ctx).Subject
|
||||
|
||||
acct, err := s.accts.CreateAccount(ctx, userID, email)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("failed to create account: %v", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
//http.Redirect(w, r, fmt.Sprintf("/site/accounts/%d", acct.ID), http.StatusSeeOther)
|
||||
http.Redirect(w, r, fmt.Sprintf("/accounts/%d", acct.ID), http.StatusSeeOther)
|
||||
}
|
||||
+27
-3
@@ -13,7 +13,7 @@ import (
|
||||
|
||||
// just keep this around long enough for testing auth middleware..
|
||||
func (s *Server) testAuthEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Println("SUCCESS:", getCustomClaims(r.Context()))
|
||||
fmt.Println("SUCCESS:", getAccessTokenClaims(r.Context()))
|
||||
|
||||
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
||||
}
|
||||
@@ -48,12 +48,36 @@ func (s *Server) authenticate(h http.Handler) http.Handler {
|
||||
return
|
||||
}
|
||||
|
||||
h.ServeHTTP(w, r.WithContext(context.WithValue(ctx, customClaimsKey{}, claims)))
|
||||
h.ServeHTTP(w, r.WithContext(setAccessTokenClaims(ctx, claims)))
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) getAccessTokenClaims(r *http.Request) (authentication.AccessTokenClaims, bool) {
|
||||
ck, err := r.Cookie("access_token")
|
||||
if err != nil {
|
||||
return authentication.AccessTokenClaims{}, false
|
||||
}
|
||||
|
||||
ctx := r.Context()
|
||||
|
||||
claims, expiration, err := s.auth.GetAccessTokenClaimsAndExpiration(ctx, ck.Value)
|
||||
if err != nil {
|
||||
return authentication.AccessTokenClaims{}, false
|
||||
}
|
||||
if expiration.Before(time.Now()) {
|
||||
return authentication.AccessTokenClaims{}, false
|
||||
}
|
||||
|
||||
return claims, true
|
||||
}
|
||||
|
||||
// stores custom claims in request context
|
||||
func setAccessTokenClaims(ctx context.Context, claims authentication.AccessTokenClaims) context.Context {
|
||||
return context.WithValue(ctx, customClaimsKey{}, claims)
|
||||
}
|
||||
|
||||
// get custom claims from request context
|
||||
func getCustomClaims(ctx context.Context) authentication.AccessTokenClaims {
|
||||
func getAccessTokenClaims(ctx context.Context) authentication.AccessTokenClaims {
|
||||
c, _ := ctx.Value(customClaimsKey{}).(authentication.AccessTokenClaims)
|
||||
return c
|
||||
}
|
||||
|
||||
@@ -48,20 +48,19 @@ func (s *Server) loginCallback(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
accessToken, expiration, err := s.auth.Exchange(ctx, q.Get("state"), q.Get("code"))
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to exchange an authorization code for a token", http.StatusUnauthorized)
|
||||
http.Error(w, fmt.Sprintf("Failed to exchange an authorization code for a token: %v", err), http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
// set access_token cookie and redirect to a reasonable place
|
||||
|
||||
w.Header().Set("Set-Cookie", (&http.Cookie{
|
||||
Name: "access_token",
|
||||
Value: accessToken,
|
||||
Path: "/",
|
||||
Expires: expiration,
|
||||
MaxAge: 0, // using Expiration instead
|
||||
Secure: true,
|
||||
SameSite: http.SameSiteStrictMode,
|
||||
Name: "access_token",
|
||||
Value: accessToken,
|
||||
Path: "/",
|
||||
Expires: expiration,
|
||||
MaxAge: 0, // using Expiration instead
|
||||
Secure: true,
|
||||
}).String())
|
||||
|
||||
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
||||
|
||||
+3
-24
@@ -93,15 +93,12 @@ func NewServer(
|
||||
s.mux.HandleFunc("GET /login", s.loginPage)
|
||||
s.mux.HandleFunc("GET /login/callback", s.loginCallback)
|
||||
s.mux.HandleFunc("GET /logout", s.logoutPage)
|
||||
s.mux.Handle("POST /accounts", s.authenticate(http.HandlerFunc(s.createAccount)))
|
||||
|
||||
// TODO: eliminate once no longer used.
|
||||
s.mux.HandleFunc("POST /login", s.login)
|
||||
|
||||
// TODO: when a user is created, we should make an account for them that is associated with their openid subject.
|
||||
// - then this can go away
|
||||
s.mux.HandleFunc("POST /accounts", s.createAccount)
|
||||
|
||||
// TODO: test the new auth middleware
|
||||
// TODO: get rid of this, once we're confident this isn't needed...
|
||||
s.mux.Handle("GET /test-auth", s.authenticate(http.HandlerFunc(s.testAuthEndpoint)))
|
||||
|
||||
// webpage content
|
||||
@@ -116,6 +113,7 @@ func NewServer(
|
||||
})))
|
||||
s.mux.Handle("GET /styles/", http.StripPrefix("/styles", http.FileServer(http.Dir(contentDir+"/styles"))))
|
||||
|
||||
// TODO: put auth on individual templates, somehow...
|
||||
s.mux.HandleFunc("GET /", s.serveTemplates)
|
||||
|
||||
return s
|
||||
@@ -125,22 +123,3 @@ func NewServer(
|
||||
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
s.mux.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
// POST /accounts
|
||||
func (s *Server) createAccount(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
email := r.FormValue("email")
|
||||
if email == "" {
|
||||
http.Error(w, "no email provided", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
acct, err := s.accts.CreateAccount(ctx, email)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("failed to create account: %v", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
//http.Redirect(w, r, fmt.Sprintf("/site/accounts/%d", acct.ID), http.StatusSeeOther)
|
||||
http.Redirect(w, r, fmt.Sprintf("/accounts/%d", acct.ID), http.StatusSeeOther)
|
||||
}
|
||||
|
||||
@@ -7,12 +7,25 @@ import (
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"ruben/inventory2/internal/domains/accounts"
|
||||
)
|
||||
|
||||
// GET /
|
||||
func (s *Server) serveTemplates(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
name, pathParams := getPageTemplateNameForURL(r.URL)
|
||||
|
||||
var (
|
||||
acct accounts.Account
|
||||
userID string
|
||||
)
|
||||
claims, ok := s.getAccessTokenClaims(r)
|
||||
if ok {
|
||||
userID = claims.Subject
|
||||
acct, _ = s.accts.GetAccountByUserID(ctx, userID)
|
||||
}
|
||||
|
||||
b, err := s.templater.ExecutePage(
|
||||
name,
|
||||
"Request",
|
||||
@@ -28,6 +41,14 @@ func (s *Server) serveTemplates(w http.ResponseWriter, r *http.Request) {
|
||||
s.accts.WithContext(ctx),
|
||||
"Etsy",
|
||||
s.etsy.WithContext(ctx),
|
||||
|
||||
// claims
|
||||
"Claims",
|
||||
claims,
|
||||
"UserID",
|
||||
userID,
|
||||
"Account",
|
||||
acct,
|
||||
)
|
||||
if err != nil {
|
||||
// TODO: handle 'not found' as a 404?
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
{{/* "dot" . */}}
|
||||
|
||||
<nav>
|
||||
<ul>
|
||||
<li>
|
||||
@@ -6,15 +8,23 @@
|
||||
</a>
|
||||
</li>
|
||||
|
||||
{{- if .dot.UserID }}
|
||||
|
||||
<li>
|
||||
<a href="/sign-up">
|
||||
Sign Up
|
||||
<a href="/accounts/{{.dot.Account.ID}}">
|
||||
Account
|
||||
</a>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<a href="/login">
|
||||
Log In
|
||||
<a href="/accounts/{{.dot.Account.ID}}/reports">
|
||||
Reports
|
||||
</a>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<a href="/accounts/{{.dot.Account.ID}}/inventory">
|
||||
Inventory
|
||||
</a>
|
||||
</li>
|
||||
|
||||
@@ -23,6 +33,16 @@
|
||||
Log Out
|
||||
</a>
|
||||
</li>
|
||||
|
||||
{{- else }}
|
||||
|
||||
<li>
|
||||
<a href="/login">
|
||||
Log In / Sign Up
|
||||
</a>
|
||||
</li>
|
||||
|
||||
{{- end }}
|
||||
</ul>
|
||||
</nav>
|
||||
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>WIP</title>
|
||||
<title>
|
||||
{{ block "title" . }} Inventory++ {{ end }}
|
||||
</title>
|
||||
|
||||
<link rel="stylesheet" href="/styles/index.css">
|
||||
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
{{- define "title" }} Inventory++ Create an Account {{ end }}
|
||||
|
||||
{{- componentBody "nav_bar" "dot" . }}
|
||||
|
||||
{{/* TODO: will need to verify the email address */}}
|
||||
|
||||
<section style="margin-top: 2em;">
|
||||
<form method="post" action="/accounts">
|
||||
<label>
|
||||
Email:
|
||||
<input type="email" required name="email" />
|
||||
</label>
|
||||
|
||||
<input type="submit" value="Create Account" />
|
||||
</form>
|
||||
</section>
|
||||
@@ -1,19 +1,19 @@
|
||||
{{ componentBody "nav_bar" }}
|
||||
{{- define "title" }} Inventory++ Account {{ end }}
|
||||
|
||||
{{- componentBody "nav_bar" "dot" . }}
|
||||
|
||||
|
||||
{{- $acctID := parseInt64 .PathParams.acctID }}
|
||||
{{- $acct := (.Accounts.GetAccount $acctID) }}
|
||||
<h1>Account: {{ .Account.Email }}</h1>
|
||||
|
||||
<h1>Account: {{ $acct.Email }} (id: {{ $acctID }})</h1>
|
||||
|
||||
{{- $etsyUser := .Etsy.GetUserPointerByAccountID $acctID }}
|
||||
{{- $etsyUser := .Etsy.GetUserPointerByAccountID .Account.ID }}
|
||||
{{- if $etsyUser }}
|
||||
<h3>Etsy User: {{ $etsyUser.UserID }}; Shop ID: {{ $etsyUser.ShopID }}</h3>
|
||||
{{- else }}
|
||||
<h3>
|
||||
<a href="{{ .Etsy.GenerateConnectionURLForNewAccount $acctID }}">
|
||||
{{/* TODO: create this link dynamically, not EVERYTIME THE PAGE IS LOADED */}}
|
||||
<a href="{{ .Etsy.GenerateConnectionURLForNewAccount .Account.ID }}">
|
||||
Link Your Etsy Store!
|
||||
</a>
|
||||
</h3>
|
||||
{{- end }}
|
||||
<h2><a href="{{$acctID}}/reports">View Reports</a></h2>
|
||||
<h2><a href="/accounts/{{.Account.ID}}/reports">View Reports</a></h2>
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
{{- define "title" }} Inventory++ {{ end }}
|
||||
|
||||
{{ componentBody "nav_bar" "dot" . }}
|
||||
|
||||
|
||||
<h1>Inventory management page: WIP</h1>
|
||||
@@ -1,4 +1,6 @@
|
||||
{{ componentBody "nav_bar" }}
|
||||
{{- define "title" }} Inventory++ Reports {{ end }}
|
||||
|
||||
{{ componentBody "nav_bar" "dot" . }}
|
||||
|
||||
{{ $storeID := .Request.URL.Query.Get "store-id" }}
|
||||
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
{{ componentBody "nav_bar" }}
|
||||
{{ componentBody "nav_bar" "dot" . }}
|
||||
|
||||
<h1>Home</h1>
|
||||
|
||||
<h2><a href="/sign-up">Sign Up!</a></h2>
|
||||
{{- if and .UserID (not .Account.ID) }}
|
||||
<h2><a href="/account-creation">New Account</a></h2>
|
||||
{{- end }}
|
||||
|
||||
<h2><a href="/test-auth">Test Auth</a></h2>
|
||||
|
||||
<a href="/test-auth">Test Auth</a>
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
{{ componentBody "nav_bar" }}
|
||||
{{/* TODO: delete this page, when certain it's not wanted anymore */}}
|
||||
|
||||
{{ componentBody "nav_bar" "dot" . }}
|
||||
|
||||
<h1>Log In</h1>
|
||||
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
{{ componentBody "nav_bar" }}
|
||||
{{/* TODO: not used - read for deletion */}}
|
||||
|
||||
{{ componentBody "nav_bar" "dot" . }}
|
||||
|
||||
<h1>Sign Up</h1>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user