Basics of Google login are implemented

This commit is contained in:
Eduard Urbach 2017-06-07 18:06:57 +02:00
parent 2f0d8c9636
commit 3cc9ba54c6
5 changed files with 79 additions and 7 deletions

View File

@ -33,6 +33,7 @@
"images/characters/arn-waifu.png" "images/characters/arn-waifu.png"
], ],
"manifest": { "manifest": {
"short_name": "notify.moe",
"theme_color": "#f8a582", "theme_color": "#f8a582",
"gcm_sender_id": "941298467524" "gcm_sender_id": "941298467524"
}, },

64
google.go Normal file
View File

@ -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)
})
}

15
main.go
View File

@ -1,8 +1,6 @@
package main package main
import ( import (
"strings"
"github.com/aerogo/aero" "github.com/aerogo/aero"
"github.com/animenotifier/notify.moe/components" "github.com/animenotifier/notify.moe/components"
"github.com/animenotifier/notify.moe/pages/airing" "github.com/animenotifier/notify.moe/pages/airing"
@ -21,6 +19,14 @@ import (
var app = aero.New() var app = aero.New()
// APIKeys ...
type APIKeys struct {
Google struct {
ID string `json:"id"`
Secret string `json:"secret"`
} `json:"google"`
}
func main() { func main() {
// CSS // CSS
app.SetStyle(components.CSS()) app.SetStyle(components.CSS())
@ -47,11 +53,12 @@ func main() {
app.Ajax("/airing", airing.Get) app.Ajax("/airing", airing.Get)
app.Ajax("/users", users.Get) app.Ajax("/users", users.Get)
EnableGoogleLogin(app)
app.Get("/images/cover/:file", func(ctx *aero.Context) string { app.Get("/images/cover/:file", func(ctx *aero.Context) string {
format := ".jpg" format := ".jpg"
accept := ctx.GetRequestHeader("Accept")
if strings.Index(accept, "image/webp") != -1 { if ctx.CanUseWebP() {
format = ".webp" format = ".webp"
} }

View File

@ -21,5 +21,5 @@ func Get(ctx *aero.Context) string {
// } // }
// return ctx.HTML(components.Dashboard(posts)) // return ctx.HTML(components.Dashboard(posts))
return ctx.HTML("ARN 4.0 is currently under construction.<br><a href='https://paypal.me/blitzprog' target='_blank' rel='noopener'>Support the development</a>") return ctx.HTML("ARN 4.0 is currently under construction.<br><a href='https://paypal.me/blitzprog' target='_blank' rel='noopener'>Support the development</a><br><a href='/auth/google'>Login via Google</a>")
} }

View File

@ -1,8 +1,8 @@
// Colors // Colors
text-color = rgb(60, 60, 60) text-color = rgb(60, 60, 60)
main-color = rgb(248, 165, 130) main-color = rgb(248, 165, 130)
link-color = rgb(245, 126, 76) link-color = rgb(230, 40, 16)
link-hover-color = rgb(242, 93, 30) link-hover-color = rgb(242, 60, 30)
link-active-color = rgb(100, 149, 237) link-active-color = rgb(100, 149, 237)
post-highlight-color = rgba(248, 165, 130, 0.7) post-highlight-color = rgba(248, 165, 130, 0.7)