Make google authentication to ersteller-lib
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
package google
|
package google_http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -7,9 +7,6 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
. "ersteller-lib"
|
. "ersteller-lib"
|
||||||
"ersteller-lib/authentication"
|
"ersteller-lib/authentication"
|
||||||
"ersteller-lib/starter/ent"
|
|
||||||
"ersteller-lib/starter/ent/user"
|
|
||||||
"ersteller-lib/starter/env"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
@@ -28,21 +25,33 @@ const oauthGoogleUrlAPI = "https://www.googleapis.com/oauth2/v2/userinfo?access_
|
|||||||
|
|
||||||
type GoogleUserData struct {
|
type GoogleUserData struct {
|
||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
Token *oauth2.Token `json:"-"`
|
Token *oauth2.Token `json:"token"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type GoogleAuth struct {
|
type GoogleAuth struct {
|
||||||
db *ent.Client
|
db Database
|
||||||
server *http.ServeMux
|
server *http.ServeMux
|
||||||
environment env.Environment
|
environment Environment
|
||||||
config oauth2.Config
|
config oauth2.Config
|
||||||
sessionStore *sessions.CookieStore
|
sessionStore *sessions.CookieStore
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewGoogleAuth(db *ent.Client, server *http.ServeMux, environment env.Environment, sessionStore *sessions.CookieStore) *GoogleAuth {
|
type Database interface {
|
||||||
|
GetUserIdByEmail(ctx context.Context, email string) (int, error)
|
||||||
|
CreateUser(ctx context.Context, email string) (int, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type Environment struct {
|
||||||
|
ClientId string
|
||||||
|
ClientSecret string
|
||||||
|
BaseUrl string
|
||||||
|
IsLocal bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewGoogleAuth(db Database, server *http.ServeMux, environment Environment, sessionStore *sessions.CookieStore) *GoogleAuth {
|
||||||
config := oauth2.Config{
|
config := oauth2.Config{
|
||||||
ClientID: environment.GoogleClientId,
|
ClientID: environment.ClientId,
|
||||||
ClientSecret: environment.GoogleClientSecret,
|
ClientSecret: environment.ClientSecret,
|
||||||
Endpoint: google.Endpoint,
|
Endpoint: google.Endpoint,
|
||||||
RedirectURL: environment.BaseUrl + GoogleLoginCallback,
|
RedirectURL: environment.BaseUrl + GoogleLoginCallback,
|
||||||
Scopes: []string{"https://www.googleapis.com/auth/userinfo.email"},
|
Scopes: []string{"https://www.googleapis.com/auth/userinfo.email"},
|
||||||
@@ -92,16 +101,15 @@ func (g *GoogleAuth) AddRoutes() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get or create user
|
// Get or create user
|
||||||
userId, err := g.db.User.Query().Where(user.Email(data.Email)).OnlyID(request.Context())
|
userId, err := g.db.GetUserIdByEmail(request.Context(), data.Email)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
LogError("Failed to get user id: %v", err)
|
LogError("Failed to get user id: %v", err)
|
||||||
newUser, err := g.db.User.Create().SetEmail(data.Email).Save(request.Context())
|
userId, err = g.db.CreateUser(request.Context(), data.Email)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
LogError("Failed to create user: %v", err)
|
LogError("Failed to create user: %v", err)
|
||||||
http.Error(writer, "Failed to create user", http.StatusInternalServerError)
|
http.Error(writer, "Failed to create user", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
userId = newUser.ID
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save email to session
|
// Save email to session
|
||||||
@@ -126,23 +134,7 @@ func (g *GoogleAuth) AddRoutes() {
|
|||||||
|
|
||||||
// Logout handler
|
// Logout handler
|
||||||
g.server.HandleFunc("GET /logout", func(writer http.ResponseWriter, request *http.Request) {
|
g.server.HandleFunc("GET /logout", func(writer http.ResponseWriter, request *http.Request) {
|
||||||
// Clear the session
|
authentication.LogoutSession(writer, request, g.sessionStore, "/")
|
||||||
session, err := g.sessionStore.Get(request, "session") // Using default session name
|
|
||||||
if err != nil {
|
|
||||||
LogError("Failed to get session: %v", err)
|
|
||||||
http.Redirect(writer, request, "/", http.StatusTemporaryRedirect)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
session.Options.MaxAge = -1
|
|
||||||
err = session.Save(request, writer)
|
|
||||||
if err != nil {
|
|
||||||
LogError("Failed to save session: %v", err)
|
|
||||||
http.Error(writer, "Failed to clear session", http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
http.Redirect(writer, request, "/", http.StatusTemporaryRedirect)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package google
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
ersteller_lib "ersteller-lib"
|
||||||
|
google_http "ersteller-lib/authentication/google/http"
|
||||||
|
"ersteller-lib/starter/ent"
|
||||||
|
"ersteller-lib/starter/ent/user"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Database struct {
|
||||||
|
db *ent.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewDatabase(db *ent.Client) google_http.Database {
|
||||||
|
return &Database{
|
||||||
|
db: db,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Database) GetUserIdByEmail(ctx context.Context, email string) (int, error) {
|
||||||
|
return d.db.User.Query().Where(user.Email(email)).OnlyID(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Database) CreateUser(ctx context.Context, email string) (int, error) {
|
||||||
|
newUser, err := d.db.User.Create().SetEmail(email).Save(ctx)
|
||||||
|
if err != nil {
|
||||||
|
ersteller_lib.LogError("Failed to create user: %v", err)
|
||||||
|
return -1, err
|
||||||
|
}
|
||||||
|
return newUser.ID, nil
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@ package routes
|
|||||||
import (
|
import (
|
||||||
. "ersteller-lib"
|
. "ersteller-lib"
|
||||||
"ersteller-lib/authentication"
|
"ersteller-lib/authentication"
|
||||||
|
google_http "ersteller-lib/authentication/google/http"
|
||||||
"ersteller-lib/starter/about"
|
"ersteller-lib/starter/about"
|
||||||
"ersteller-lib/starter/contact"
|
"ersteller-lib/starter/contact"
|
||||||
"ersteller-lib/starter/ent"
|
"ersteller-lib/starter/ent"
|
||||||
@@ -29,7 +30,12 @@ func CreateApi(environment env.Environment, db *ent.Client) http.Handler {
|
|||||||
sessionStore.Options.HttpOnly = true
|
sessionStore.Options.HttpOnly = true
|
||||||
}
|
}
|
||||||
|
|
||||||
googleAuth := google.NewGoogleAuth(db, server.GetHttpServer(), environment, sessionStore)
|
googleAuth := google_http.NewGoogleAuth(google.NewDatabase(db), server.GetHttpServer(), google_http.Environment{
|
||||||
|
ClientId: environment.GoogleClientId,
|
||||||
|
ClientSecret: environment.GoogleClientSecret,
|
||||||
|
IsLocal: environment.IsLocal,
|
||||||
|
BaseUrl: environment.BaseUrl,
|
||||||
|
}, sessionStore)
|
||||||
googleAuth.AddRoutes()
|
googleAuth.AddRoutes()
|
||||||
|
|
||||||
indexActivePath := NewActivePath(map[Language]string{
|
indexActivePath := NewActivePath(map[Language]string{
|
||||||
@@ -83,7 +89,7 @@ func CreateApi(environment env.Environment, db *ent.Client) http.Handler {
|
|||||||
|
|
||||||
serverWithMiddleWare := UseMiddleware(server, LoggingMiddleware, MakeGzipHandler,
|
serverWithMiddleWare := UseMiddleware(server, LoggingMiddleware, MakeGzipHandler,
|
||||||
authentication.Middleware(sessionStore,
|
authentication.Middleware(sessionStore,
|
||||||
[]string{"/de" + login.LoginPathDe, "/en" + authentication.LoginPath, google.GoogleLogin, google.GoogleLoginCallback, "/static"}, loginPaths))
|
[]string{"/de" + login.LoginPathDe, "/en" + authentication.LoginPath, google_http.GoogleLogin, google_http.GoogleLoginCallback, "/static"}, loginPaths))
|
||||||
|
|
||||||
return serverWithMiddleWare
|
return serverWithMiddleWare
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user