Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

show gravatar and allow logout

goenning414e2929a52e
diff --git a/app/handler/auth.go b/app/handler/auth.go
index 9de4ae7f1..a75fe8a85 100644
--- a/app/handler/auth.go
+++ b/app/handler/auth.go
@@ -3,6 +3,7 @@ package handler
import (
"net/http"
"net/url"
+ "time"
"golang.org/x/oauth2"
@@ -106,3 +107,15 @@ func (h OAuthHandlers) Login(provider string) echo.HandlerFunc {
return c.Redirect(http.StatusTemporaryRedirect, authURL)
}
}
+
+// Logout remove auth cookies
+func (h OAuthHandlers) Logout() echo.HandlerFunc {
+ return func(c echo.Context) error {
+ c.SetCookie(&http.Cookie{
+ Name: "auth",
+ MaxAge: -1,
+ Expires: time.Now().Add(-100 * time.Hour),
+ })
+ return c.Redirect(http.StatusTemporaryRedirect, c.QueryParam("redirect"))
+ }
+}
diff --git a/app/router/middleware/hostchecker.go b/app/router/middleware/hostchecker.go
index f801045fe..d8b78eea4 100644
--- a/app/router/middleware/hostchecker.go
+++ b/app/router/middleware/hostchecker.go
@@ -3,16 +3,19 @@ package middleware
import (
"net/http"
+ "net/url"
+
"github.com/labstack/echo"
)
// HostChecker checks for a specific host
-func HostChecker(host string) echo.MiddlewareFunc {
+func HostChecker(baseURL string) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
+ u, _ := url.Parse(baseURL)
- if c.Request().Host != host {
- c.Logger().Errorf("%s is not valid for this operation. Only %s is allowed.", c.Request().Host, host)
+ if c.Request().Host != u.Host {
+ c.Logger().Errorf("%s is not valid for this operation. Only %s is allowed.", c.Request().Host, u.Host)
return c.NoContent(http.StatusBadRequest)
}
diff --git a/app/router/renderer.go b/app/router/renderer.go
index 3acc91671..3bc61bbf4 100644
--- a/app/router/renderer.go
+++ b/app/router/renderer.go
@@ -7,7 +7,9 @@ import (
"os"
+ "github.com/WeCanHearYou/wchy/app/auth"
"github.com/WeCanHearYou/wchy/app/env"
+ "github.com/WeCanHearYou/wchy/app/util"
"github.com/labstack/echo"
)
@@ -62,8 +64,13 @@ func (r *HTMLRenderer) Render(w io.Writer, name string, data interface{}, c echo
//TODO: refactor (and move somewhere else?)
m := data.(echo.Map)
+ claims, ok := c.Get("Claims").(*auth.WchyClaims)
+
m["AuthEndpoint"] = os.Getenv("AUTH_ENDPOINT")
- m["Claims"] = c.Get("Claims")
+ if ok {
+ m["Claims"] = claims
+ m["Gravatar"] = util.MD5(claims.UserEmail)
+ }
m["CurrentUrl"] = protocol + c.Request().Host + c.Request().URL.String()
return tmpl.Execute(w, m)
diff --git a/app/router/router.go b/app/router/router.go
index ad7269d31..badd35360 100644
--- a/app/router/router.go
+++ b/app/router/router.go
@@ -64,6 +64,7 @@ func GetMainEngine(ctx *context.WchyContext) *echo.Echo {
app.Use(middleware.MultiTenant(ctx))
app.GET("/", handler.Index(ctx))
+ app.GET("/logout", handler.OAuth(ctx).Logout())
api := app.Group("/api")
{
diff --git a/app/util/crypto.go b/app/util/crypto.go
new file mode 100644
index 000000000..ec84f9180
--- /dev/null
+++ b/app/util/crypto.go
@@ -0,0 +1,14 @@
+package util
+
+import (
+ "crypto/md5"
+ "encoding/hex"
+ "fmt"
+)
+
+//MD5 encrypt content with MD5 algorithm
+func MD5(content string) string {
+ hasher := md5.New()
+ hasher.Write([]byte(content))
+ return fmt.Sprintf("%v", hex.EncodeToString(hasher.Sum(nil)))
+}
diff --git a/views/index.html b/views/index.html
index 0e0a8cab1..dac6219e6 100644
--- a/views/index.html
+++ b/views/index.html
@@ -15,30 +15,43 @@
<i class="dropdown icon"></i>
</a>
{{ else }}
- <a class="item right">
- <img class="ui avatar image" src="https://www.gravatar.com/avatar/">
+ <a class="item right signin">
+ <img class="ui avatar image" src="https://www.gravatar.com/avatar/{{ .Gravatar }}">
{{ .Claims.UserName }}
+ <i class="dropdown icon"></i>
</a>
{{ end }}
</div>
</div>
- <div id="user-popup" class="ui popup top left transition hidden">
- <div class="ui list">
- <div class="item">
- <a href="{{ .AuthEndpoint }}/oauth/facebook?redirect={{ .CurrentUrl }}" class="ui fluid facebook button">
- <i class="facebook icon"></i>
- Sign in with Facebook
- </a>
+ {{if not .Claims }}
+ <div id="user-popup" class="ui popup top left transition hidden">
+ <div class="ui list">
+ <div class="item">
+ <a href="{{ .AuthEndpoint }}/oauth/facebook?redirect={{ .CurrentUrl }}" class="ui fluid facebook button">
+ <i class="facebook icon"></i>
+ Sign in with Facebook
+ </a>
+ </div>
+ <div class="item">
+ <a href="{{ .AuthEndpoint }}/oauth/google?redirect={{ .CurrentUrl }}" class="ui fluid google plus button">
+ <i class="google icon"></i>
+ Sign in with Google
+ </a>
+ </div>
</div>
- <div class="item">
- <a href="{{ .AuthEndpoint }}/oauth/google?redirect={{ .CurrentUrl }}" class="ui fluid google plus button">
- <i class="google icon"></i>
- Sign in with Google
- </a>
+ </div>
+ {{ else }}
+ <div id="user-popup" class="ui popup top left transition hidden">
+ <div class="ui">
+ <div class="item">
+ <a href="/logout?redirect=/">
+ Sign out
+ </a>
+ </div>
</div>
</div>
- </div>
+ {{ end }}
<div class="ui container">
<h1 class="ui header">Welcome to our feedback forum</h1>