This commit is contained in:
2015-03-05 22:04:44 +07:00
parent b070f19510
commit 317ee6e075
7 changed files with 131 additions and 35 deletions
+8 -5
View File
@@ -12,6 +12,10 @@ import (
)
func authHelloHandler(w http.ResponseWriter, r *http.Request) {
if currentUser(r) != "" {
http.Redirect(w, r, "/app", 302)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf8")
helloTmpl.ExecuteTemplate(w, "hello", map[string]interface{}{})
}
@@ -20,7 +24,7 @@ func authSigninHandler(w http.ResponseWriter, r *http.Request) {
params := url.Values{}
params.Set("client_id", config.C().ClientID)
params.Set("redirect_uri", config.C().RedirectURI)
params.Set("scope", "repo")
params.Set("scope", "read:org, repo, admin:org_hook")
http.Redirect(w, r, config.C().AuthURL+"?"+params.Encode(), 302)
}
@@ -38,11 +42,10 @@ func authCallbackHandler(w http.ResponseWriter, r *http.Request) {
Task: &db.Task{},
})
if token, ok := <-res; ok {
fmt.Println("Got access token: ", token)
w.Write([]byte(token))
if login, ok := <-res; ok {
authorize(r, login)
} else {
panic("Failed to fetch token")
panic("Failed to access token or user info")
}
}
}
-24
View File
@@ -4,16 +4,10 @@ import (
"fmt"
"html/template"
"net/http"
"time"
"code.google.com/p/go-uuid/uuid"
"github.com/GeertJohan/go.rice"
)
const (
sessionCookie = "session_id"
)
var (
helloTmpl = template.New("hello")
)
@@ -34,21 +28,3 @@ func Start() {
fmt.Println("Starting server at http://localhost:8080")
http.ListenAndServe(":8080", nil)
}
func sessionHandler(w http.ResponseWriter, r *http.Request) {
if cook, err := r.Cookie(sessionCookie); err != nil {
cook = &http.Cookie{
Name: sessionCookie,
Value: uuid.New(),
Path: "/",
Expires: time.Now().Add(365 * 24 * time.Hour),
HttpOnly: true,
}
http.SetCookie(w, cook)
}
}
func sessionID(r *http.Request) string {
cook, _ := r.Cookie(sessionCookie)
return cook.Value
}
+50
View File
@@ -0,0 +1,50 @@
package server
import (
"net/http"
"time"
"code.google.com/p/go-uuid/uuid"
"github.com/garyburd/redigo/redis"
)
const (
sessionCookie = "session_id"
)
var (
redisC = redis.NewPool(dialRedis, 10)
)
func dialRedis() (redis.Conn, error) {
return redis.Dial("tcp", ":6379")
}
func sessionHandler(w http.ResponseWriter, r *http.Request) {
if cook, err := r.Cookie(sessionCookie); err != nil {
cook = &http.Cookie{
Name: sessionCookie,
Value: uuid.New(),
Path: "/",
Expires: time.Now().Add(365 * 24 * time.Hour),
HttpOnly: true,
}
http.SetCookie(w, cook)
}
}
func sessionID(r *http.Request) string {
cook, _ := r.Cookie(sessionCookie)
return cook.Value
}
func currentUser(r *http.Request) string {
conn := redisC.Get()
login, _ := redis.String(conn.Do("HGET", "sessions", sessionID(r)))
return login
}
func authorize(r *http.Request, login string) {
redisC.Get().Do("HSET", "sessions", sessionID(r), login)
}