Include identities to user claims
This commit is contained in:
parent
5a2083aef0
commit
59ff04dbbc
12
handlers.go
12
handlers.go
|
@ -26,6 +26,12 @@ func NewHandler(oauth2Config *oauth2.Config, session SessionStorer, verifier *oi
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Identity struct {
|
||||||
|
UserID string `json:"userId"`
|
||||||
|
ProviderName string `json:"providerName"`
|
||||||
|
ProviderType string `json:"providerType"`
|
||||||
|
}
|
||||||
|
|
||||||
type UserClaims struct {
|
type UserClaims struct {
|
||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
Verified bool `json:"email_verified"`
|
Verified bool `json:"email_verified"`
|
||||||
|
@ -34,6 +40,7 @@ type UserClaims struct {
|
||||||
Picture string `json:"picture"`
|
Picture string `json:"picture"`
|
||||||
Sub string `json:"sub"`
|
Sub string `json:"sub"`
|
||||||
Groups []string `json:"cognito:groups"`
|
Groups []string `json:"cognito:groups"`
|
||||||
|
Identities []*Identity `json:"identities"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateState() (string, error) {
|
func generateState() (string, error) {
|
||||||
|
@ -48,19 +55,21 @@ func generateState() (string, error) {
|
||||||
func (h *Handlers) SignIn(w http.ResponseWriter, r *http.Request) {
|
func (h *Handlers) SignIn(w http.ResponseWriter, r *http.Request) {
|
||||||
state, err := generateState()
|
state, err := generateState()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Failed to generate state")
|
log.Printf("Failed to generate state: %v", err)
|
||||||
http.Error(w, "Something went wrong", http.StatusInternalServerError)
|
http.Error(w, "Something went wrong", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
session, err := h.session.Get(r)
|
session, err := h.session.Get(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Printf("Failed to get session: %v", err)
|
||||||
http.Error(w, "Failed to get session", http.StatusInternalServerError)
|
http.Error(w, "Failed to get session", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
session.Values["state"] = state
|
session.Values["state"] = state
|
||||||
err = session.Save(r, w)
|
err = session.Save(r, w)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Printf("Failed to save session: %v", err)
|
||||||
http.Error(w, "Failed to save session", http.StatusInternalServerError)
|
http.Error(w, "Failed to save session", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -74,6 +83,7 @@ func (h *Handlers) CallbackHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
session, err := h.session.Get(r)
|
session, err := h.session.Get(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Printf("Failed to verify ID Token: %v", err)
|
||||||
http.Error(w, "Failed to get session", http.StatusInternalServerError)
|
http.Error(w, "Failed to get session", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,13 +3,14 @@ package cauth
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/gob"
|
"encoding/gob"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/coreos/go-oidc/v3/oidc"
|
"github.com/coreos/go-oidc/v3/oidc"
|
||||||
"github.com/gorilla/sessions"
|
"github.com/gorilla/sessions"
|
||||||
"github.com/rbcervilla/redisstore/v9"
|
"github.com/rbcervilla/redisstore/v9"
|
||||||
"github.com/redis/go-redis/v9"
|
"github.com/redis/go-redis/v9"
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
"log"
|
|
||||||
"net/http"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const SESSION_NAME = "auth-session"
|
const SESSION_NAME = "auth-session"
|
||||||
|
@ -31,6 +32,7 @@ type SessionStorer interface {
|
||||||
func NewRedisSessionStore(params RedisSessionParams) (SessionStorer, error) {
|
func NewRedisSessionStore(params RedisSessionParams) (SessionStorer, error) {
|
||||||
gob.Register(&oauth2.Token{})
|
gob.Register(&oauth2.Token{})
|
||||||
gob.Register(oidc.IDToken{})
|
gob.Register(oidc.IDToken{})
|
||||||
|
gob.Register(Identity{})
|
||||||
gob.Register(UserClaims{})
|
gob.Register(UserClaims{})
|
||||||
client := redis.NewClient(&redis.Options{
|
client := redis.NewClient(&redis.Options{
|
||||||
Addr: params.RedisAddress,
|
Addr: params.RedisAddress,
|
||||||
|
|
Loading…
Reference in New Issue