From ad9870b1c1010f38e843fac31ebed791f76964f9 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Sun, 17 Nov 2019 16:48:27 +0900 Subject: [PATCH] Improved authentication --- auth/{facebook.go => Facebook.go} | 5 +++-- auth/{google.go => Google.go} | 5 +++-- auth/Install.go | 25 +++++++++++++++++++++++++ auth/{auth.go => Logout.go} | 18 ++++-------------- auth/{twitter.go => Twitter.go} | 5 +++-- auth/log.go | 14 -------------- 6 files changed, 38 insertions(+), 34 deletions(-) rename auth/{facebook.go => Facebook.go} (97%) rename auth/{google.go => Google.go} (97%) create mode 100644 auth/Install.go rename auth/{auth.go => Logout.go} (63%) rename auth/{twitter.go => Twitter.go} (97%) delete mode 100644 auth/log.go diff --git a/auth/facebook.go b/auth/Facebook.go similarity index 97% rename from auth/facebook.go rename to auth/Facebook.go index d617ca03..35ee33ee 100644 --- a/auth/facebook.go +++ b/auth/Facebook.go @@ -8,6 +8,7 @@ import ( "strings" "github.com/aerogo/aero" + "github.com/aerogo/log" "github.com/animenotifier/notify.moe/arn" "github.com/animenotifier/notify.moe/assets" "github.com/animenotifier/notify.moe/utils" @@ -25,8 +26,8 @@ type FacebookUser struct { Gender string `json:"gender"` } -// InstallFacebookAuth enables Facebook login for the app. -func InstallFacebookAuth(app *aero.Application) { +// Facebook enables Facebook login for the app. +func Facebook(app *aero.Application, authLog *log.Log) { // OAuth2 configuration defines the API keys, // scopes of required data and the redirect URL // that Facebook should send the user to after diff --git a/auth/google.go b/auth/Google.go similarity index 97% rename from auth/google.go rename to auth/Google.go index d8b60299..e693187b 100644 --- a/auth/google.go +++ b/auth/Google.go @@ -8,6 +8,7 @@ import ( "strings" "github.com/aerogo/aero" + "github.com/aerogo/log" "github.com/animenotifier/notify.moe/arn" "github.com/animenotifier/notify.moe/assets" "github.com/animenotifier/notify.moe/utils" @@ -30,8 +31,8 @@ type GoogleUser struct { // EmailVerified bool `json:"email_verified"` } -// InstallGoogleAuth enables Google login for the app. -func InstallGoogleAuth(app *aero.Application) { +// Google enables Google login for the app. +func Google(app *aero.Application, authLog *log.Log) { // OAuth2 configuration defines the API keys, // scopes of required data and the redirect URL // that Google should send the user to after diff --git a/auth/Install.go b/auth/Install.go new file mode 100644 index 00000000..41cfa43b --- /dev/null +++ b/auth/Install.go @@ -0,0 +1,25 @@ +package auth + +import ( + "os" + + "github.com/aerogo/aero" + "github.com/aerogo/log" +) + +const newUserStartRoute = "/welcome" + +// Install installs all authentication routes in the application. +func Install(app *aero.Application) { + authLog := log.New() + authLog.AddWriter(os.Stdout) + authLog.AddWriter(log.File("logs/auth.log")) + + // Login + Google(app, authLog) + Facebook(app, authLog) + Twitter(app, authLog) + + // Logout + Logout(app, authLog) +} diff --git a/auth/auth.go b/auth/Logout.go similarity index 63% rename from auth/auth.go rename to auth/Logout.go index a292b235..d9f5a632 100644 --- a/auth/auth.go +++ b/auth/Logout.go @@ -4,23 +4,13 @@ import ( "net/http" "github.com/aerogo/aero" + "github.com/aerogo/log" "github.com/animenotifier/notify.moe/utils" ) -const newUserStartRoute = "/welcome" - -// Install installs the authentication routes in the application. -func Install(app *aero.Application) { - // Google - InstallGoogleAuth(app) - - // Facebook - InstallFacebookAuth(app) - - // Twitter - InstallTwitterAuth(app) - - // Logout +// Logout is called when the user clicks the logout button. +// It deletes the "userId" from the session. +func Logout(app *aero.Application, authLog *log.Log) { app.Get("/logout", func(ctx aero.Context) error { if ctx.HasSession() { user := utils.GetUser(ctx) diff --git a/auth/twitter.go b/auth/Twitter.go similarity index 97% rename from auth/twitter.go rename to auth/Twitter.go index 6a28fa2a..5f727788 100644 --- a/auth/twitter.go +++ b/auth/Twitter.go @@ -9,6 +9,7 @@ import ( "strings" "github.com/aerogo/aero" + "github.com/aerogo/log" "github.com/animenotifier/notify.moe/arn" "github.com/animenotifier/notify.moe/assets" "github.com/animenotifier/notify.moe/utils" @@ -25,8 +26,8 @@ type TwitterUser struct { ScreenName string `json:"screen_name"` } -// InstallTwitterAuth enables Twitter login for the app. -func InstallTwitterAuth(app *aero.Application) { +// Twitter enables Twitter login for the app. +func Twitter(app *aero.Application, authLog *log.Log) { // oauth1 configuration defines the API keys, // the url for the request token, the access token and the authorisation. config := &oauth.Client{ diff --git a/auth/log.go b/auth/log.go deleted file mode 100644 index e75e0d31..00000000 --- a/auth/log.go +++ /dev/null @@ -1,14 +0,0 @@ -package auth - -import ( - "os" - - "github.com/aerogo/log" -) - -var authLog = log.New() - -func init() { - authLog.AddWriter(os.Stdout) - authLog.AddWriter(log.File("logs/auth.log")) -}