From 3cc9ba54c6ef8de57b4fc889b17544580104b48c Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Wed, 7 Jun 2017 18:06:57 +0200 Subject: [PATCH] Basics of Google login are implemented --- config.json | 1 + google.go | 64 +++++++++++++++++++++++++++++++++++ main.go | 15 +++++--- pages/dashboard/dashboard.go | 2 +- styles/include/config.scarlet | 4 +-- 5 files changed, 79 insertions(+), 7 deletions(-) create mode 100644 google.go diff --git a/config.json b/config.json index 48fd8390..a2101020 100644 --- a/config.json +++ b/config.json @@ -33,6 +33,7 @@ "images/characters/arn-waifu.png" ], "manifest": { + "short_name": "notify.moe", "theme_color": "#f8a582", "gcm_sender_id": "941298467524" }, diff --git a/google.go b/google.go new file mode 100644 index 00000000..1e8d2eb6 --- /dev/null +++ b/google.go @@ -0,0 +1,64 @@ +package main + +import ( + "encoding/json" + "errors" + "io/ioutil" + "log" + "net/http" + + "github.com/aerogo/aero" + "golang.org/x/oauth2" + "golang.org/x/oauth2/google" +) + +// EnableGoogleLogin enables Google login for the app. +func EnableGoogleLogin(app *aero.Application) { + var api APIKeys + data, _ := ioutil.ReadFile("security/api-keys.json") + json.Unmarshal(data, &api) + + conf := &oauth2.Config{ + ClientID: api.Google.ID, + ClientSecret: api.Google.Secret, + RedirectURL: "https://beta.notify.moe/auth/google/callback", + Scopes: []string{ + "https://www.googleapis.com/auth/userinfo.email", + }, + Endpoint: google.Endpoint, + } + + // Auth + app.Get("/auth/google", func(ctx *aero.Context) string { + url := conf.AuthCodeURL(ctx.Session().ID()) + ctx.Redirect(url) + return "" + }) + + // Auth Callback + app.Get("/auth/google/callback", func(ctx *aero.Context) string { + if ctx.Session().ID() != ctx.Query("state") { + return ctx.Error(http.StatusBadRequest, "Authorization not allowed for this session", errors.New("Google login failed: Incorrect state")) + } + + // Handle the exchange code to initiate a transport + token, err := conf.Exchange(oauth2.NoContext, ctx.Query("code")) + if err != nil { + return ctx.Error(http.StatusBadRequest, "Could not obtain OAuth token", err) + } + + // Construct the OAuth client + client := conf.Client(oauth2.NoContext, token) + + resp, err := client.Get("https://www.googleapis.com/oauth2/v3/userinfo") + if err != nil { + return ctx.Error(http.StatusBadRequest, "Failed requesting user data from Google", err) + } + defer resp.Body.Close() + dataBytes, _ := ioutil.ReadAll(resp.Body) + data := string(dataBytes) + log.Println("Resp body: ", data) + + return ctx.Text(data) + }) +} diff --git a/main.go b/main.go index 50e2f409..436d85bb 100644 --- a/main.go +++ b/main.go @@ -1,8 +1,6 @@ package main import ( - "strings" - "github.com/aerogo/aero" "github.com/animenotifier/notify.moe/components" "github.com/animenotifier/notify.moe/pages/airing" @@ -21,6 +19,14 @@ import ( var app = aero.New() +// APIKeys ... +type APIKeys struct { + Google struct { + ID string `json:"id"` + Secret string `json:"secret"` + } `json:"google"` +} + func main() { // CSS app.SetStyle(components.CSS()) @@ -47,11 +53,12 @@ func main() { app.Ajax("/airing", airing.Get) app.Ajax("/users", users.Get) + EnableGoogleLogin(app) + app.Get("/images/cover/:file", func(ctx *aero.Context) string { format := ".jpg" - accept := ctx.GetRequestHeader("Accept") - if strings.Index(accept, "image/webp") != -1 { + if ctx.CanUseWebP() { format = ".webp" } diff --git a/pages/dashboard/dashboard.go b/pages/dashboard/dashboard.go index 0a051020..c4c4bb42 100644 --- a/pages/dashboard/dashboard.go +++ b/pages/dashboard/dashboard.go @@ -21,5 +21,5 @@ func Get(ctx *aero.Context) string { // } // return ctx.HTML(components.Dashboard(posts)) - return ctx.HTML("ARN 4.0 is currently under construction.
Support the development") + return ctx.HTML("ARN 4.0 is currently under construction.
Support the development
Login via Google") } diff --git a/styles/include/config.scarlet b/styles/include/config.scarlet index d3b62ce5..72ae4410 100644 --- a/styles/include/config.scarlet +++ b/styles/include/config.scarlet @@ -1,8 +1,8 @@ // Colors text-color = rgb(60, 60, 60) main-color = rgb(248, 165, 130) -link-color = rgb(245, 126, 76) -link-hover-color = rgb(242, 93, 30) +link-color = rgb(230, 40, 16) +link-hover-color = rgb(242, 60, 30) link-active-color = rgb(100, 149, 237) post-highlight-color = rgba(248, 165, 130, 0.7)