Make google authentication to ersteller-lib
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package google
|
||||
package google_http
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -7,9 +7,6 @@ import (
|
||||
"encoding/json"
|
||||
. "ersteller-lib"
|
||||
"ersteller-lib/authentication"
|
||||
"ersteller-lib/starter/ent"
|
||||
"ersteller-lib/starter/ent/user"
|
||||
"ersteller-lib/starter/env"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"time"
|
||||
@@ -28,21 +25,33 @@ const oauthGoogleUrlAPI = "https://www.googleapis.com/oauth2/v2/userinfo?access_
|
||||
|
||||
type GoogleUserData struct {
|
||||
Email string `json:"email"`
|
||||
Token *oauth2.Token `json:"-"`
|
||||
Token *oauth2.Token `json:"token"`
|
||||
}
|
||||
|
||||
type GoogleAuth struct {
|
||||
db *ent.Client
|
||||
db Database
|
||||
server *http.ServeMux
|
||||
environment env.Environment
|
||||
environment Environment
|
||||
config oauth2.Config
|
||||
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{
|
||||
ClientID: environment.GoogleClientId,
|
||||
ClientSecret: environment.GoogleClientSecret,
|
||||
ClientID: environment.ClientId,
|
||||
ClientSecret: environment.ClientSecret,
|
||||
Endpoint: google.Endpoint,
|
||||
RedirectURL: environment.BaseUrl + GoogleLoginCallback,
|
||||
Scopes: []string{"https://www.googleapis.com/auth/userinfo.email"},
|
||||
@@ -92,16 +101,15 @@ func (g *GoogleAuth) AddRoutes() {
|
||||
}
|
||||
|
||||
// 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 {
|
||||
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 {
|
||||
LogError("Failed to create user: %v", err)
|
||||
http.Error(writer, "Failed to create user", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
userId = newUser.ID
|
||||
}
|
||||
|
||||
// Save email to session
|
||||
@@ -126,23 +134,7 @@ func (g *GoogleAuth) AddRoutes() {
|
||||
|
||||
// Logout handler
|
||||
g.server.HandleFunc("GET /logout", func(writer http.ResponseWriter, request *http.Request) {
|
||||
// Clear the session
|
||||
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)
|
||||
authentication.LogoutSession(writer, request, g.sessionStore, "/")
|
||||
})
|
||||
}
|
||||
|
||||
@@ -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 (
|
||||
. "ersteller-lib"
|
||||
"ersteller-lib/authentication"
|
||||
google_http "ersteller-lib/authentication/google/http"
|
||||
"ersteller-lib/starter/about"
|
||||
"ersteller-lib/starter/contact"
|
||||
"ersteller-lib/starter/ent"
|
||||
@@ -29,7 +30,12 @@ func CreateApi(environment env.Environment, db *ent.Client) http.Handler {
|
||||
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()
|
||||
|
||||
indexActivePath := NewActivePath(map[Language]string{
|
||||
@@ -83,7 +89,7 @@ func CreateApi(environment env.Environment, db *ent.Client) http.Handler {
|
||||
|
||||
serverWithMiddleWare := UseMiddleware(server, LoggingMiddleware, MakeGzipHandler,
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user