Récupération automatique du profil au lancement de l'application

This commit is contained in:
2020-07-13 18:49:44 +02:00
parent 3bcebdfcd1
commit 8b8f322630
16 changed files with 349 additions and 46 deletions

View File

@ -37,13 +37,19 @@ func NewFromFile(filepath string) (*Config, error) {
}
type HTTPConfig struct {
Address string `yaml:"address" env:"HTTP_ADDRESS"`
CookieAuthenticationKey string `yaml:"cookieAuthenticationKey" env:"HTTP_COOKIE_AUTHENTICATION_KEY"`
CookieEncryptionKey string `yaml:"cookieEncryptionKey" env:"HTTP_COOKIE_ENCRYPTION_KEY"`
CookieMaxAge int `yaml:"cookieMaxAge" env:"HTTP_COOKIE_MAX_AGE"`
TemplateDir string `yaml:"templateDir" env:"HTTP_TEMPLATE_DIR"`
PublicDir string `yaml:"publicDir" env:"HTTP_PUBLIC_DIR"`
FrontendURL string `yaml:"frontendURL" env:"HTTP_FRONTEND_URL"`
Address string `yaml:"address" env:"HTTP_ADDRESS"`
CookieAuthenticationKey string `yaml:"cookieAuthenticationKey" env:"HTTP_COOKIE_AUTHENTICATION_KEY"`
CookieEncryptionKey string `yaml:"cookieEncryptionKey" env:"HTTP_COOKIE_ENCRYPTION_KEY"`
CookieMaxAge int `yaml:"cookieMaxAge" env:"HTTP_COOKIE_MAX_AGE"`
TemplateDir string `yaml:"templateDir" env:"HTTP_TEMPLATE_DIR"`
PublicDir string `yaml:"publicDir" env:"HTTP_PUBLIC_DIR"`
FrontendURL string `yaml:"frontendURL" env:"HTTP_FRONTEND_URL"`
CORS CORSConfig `yaml:"cors"`
}
type CORSConfig struct {
AllowedOrigins []string `yaml:"allowedOrigins" env:"HTTP_CORS_ALLOWED_ORIGINS"`
AllowCredentials bool `yaml:"allowCredentials" env:"HTTP_CORS_ALLOW_CREDENTIALS"`
}
type OIDCConfig struct {
@ -83,6 +89,10 @@ func NewDefault() *Config {
TemplateDir: "template",
PublicDir: "public",
FrontendURL: "http://localhost:8080",
CORS: CORSConfig{
AllowedOrigins: []string{"http://localhost:8080"},
AllowCredentials: true,
},
},
OIDC: OIDCConfig{
IssuerURL: "http://localhost:4444/",

View File

@ -3,6 +3,9 @@ package route
import (
"net/http"
"forge.cadoles.com/Cadoles/daddy/internal/session"
"github.com/pkg/errors"
"forge.cadoles.com/Cadoles/daddy/internal/config"
oidc "forge.cadoles.com/wpetit/goweb-oidc"
"gitlab.com/wpetit/goweb/logger"
@ -21,6 +24,10 @@ func handleLogout(w http.ResponseWriter, r *http.Request) {
logger.F("postLogoutURL", conf.OIDC.PostLogoutRedirectURL),
)
if err := session.ClearUserEmail(w, r, false); err != nil {
panic(errors.WithStack(err))
}
client.Logout(w, r, conf.OIDC.PostLogoutRedirectURL)
}

View File

@ -1,15 +1,22 @@
package route
import (
"net/http"
"time"
"forge.cadoles.com/Cadoles/daddy/internal/config"
"forge.cadoles.com/Cadoles/daddy/internal/graph"
"forge.cadoles.com/Cadoles/daddy/internal/graph/generated"
"forge.cadoles.com/Cadoles/daddy/internal/session"
oidc "forge.cadoles.com/wpetit/goweb-oidc"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/handler/extension"
"github.com/99designs/gqlgen/graphql/handler/transport"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/gorilla/websocket"
"github.com/go-chi/chi"
"github.com/rs/cors"
"gitlab.com/wpetit/goweb/static"
)
@ -21,20 +28,39 @@ func Mount(r *chi.Mux, config *config.Config) error {
r.Get("/logout/redirect", handleLogoutRedirect)
r.Route("/api", func(r chi.Router) {
r.Use(cors.New(cors.Options{
AllowedOrigins: config.HTTP.CORS.AllowedOrigins,
AllowCredentials: config.HTTP.CORS.AllowCredentials,
Debug: config.Debug,
}).Handler)
r.Use(oidc.Middleware)
r.Use(session.UserEmailMiddleware)
gql := handler.NewDefaultServer(
gql := handler.New(
generated.NewExecutableSchema(generated.Config{
Resolvers: &graph.Resolver{},
}),
)
gql.AddTransport(transport.POST{})
gql.AddTransport(&transport.Websocket{
KeepAlivePingInterval: 10 * time.Second,
Upgrader: websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
// TODO Check WS connection origin
return true
},
ReadBufferSize: 1024,
WriteBufferSize: 1024,
},
})
if config.Debug {
r.Get("/v1/graphql", playground.Handler("GraphQL playground", "/api/v1/graphql"))
gql.Use(extension.Introspection{})
r.Get("/v1/playground", playground.Handler("GraphQL playground", "/api/v1/graphql"))
}
r.Post("/v1/graphql", gql.ServeHTTP)
r.Handle("/v1/graphql", gql)
})
notFoundHandler := r.NotFoundHandler()

View File

@ -61,6 +61,23 @@ func SaveUserEmail(w http.ResponseWriter, r *http.Request, email string) error {
return nil
}
func ClearUserEmail(w http.ResponseWriter, r *http.Request, saveSession bool) error {
sess, err := getSession(w, r)
if err != nil {
return errors.WithStack(err)
}
sess.Unset(string(userEmailKey))
if saveSession {
if err := sess.Save(w, r); err != nil {
return errors.WithStack(err)
}
}
return nil
}
func GetUserEmail(w http.ResponseWriter, r *http.Request) (string, error) {
sess, err := getSession(w, r)
if err != nil {