empact/server/auth.go

42 lines
974 B
Go
Raw Normal View History

2015-03-05 14:46:19 +07:00
package server
import (
2015-03-06 19:36:35 +07:00
"log"
2015-03-05 14:46:19 +07:00
"net/http"
"net/url"
2015-03-05 15:07:18 +07:00
"github.com/localhots/empact/config"
2015-03-05 19:57:36 +07:00
"github.com/localhots/empact/task"
2015-03-05 14:46:19 +07:00
)
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)
2015-03-05 22:04:44 +07:00
params.Set("scope", "read:org, repo, admin:org_hook")
2015-03-05 19:57:36 +07:00
http.Redirect(w, r, config.C().AuthURL+"?"+params.Encode(), 302)
2015-03-05 14:46:19 +07:00
}
func authCallbackHandler(w http.ResponseWriter, r *http.Request) {
2015-03-07 21:56:02 +07:00
req, _ := parseRequest(w, r)
2015-03-05 14:46:19 +07:00
if r.FormValue("error") != "" {
w.Write([]byte(r.FormValue("error_description")))
2015-03-06 19:36:35 +07:00
return
}
2015-03-05 19:57:36 +07:00
2015-03-06 19:36:35 +07:00
code := r.FormValue("code")
2015-03-06 20:23:01 +07:00
log.Printf("Got code %q\n", code)
2015-03-07 21:56:02 +07:00
if token, login, err := task.Authenticate(code); err == nil {
req.authorize(token, login)
2015-03-06 19:36:35 +07:00
} else {
panic(err)
2015-03-05 14:46:19 +07:00
}
}
2015-03-05 22:25:26 +07:00
func authHandler(w http.ResponseWriter, r *http.Request) {
2015-03-07 21:56:02 +07:00
if req, _ := parseRequest(w, r); req.login == "" {
http.Redirect(w, r, "/", 302)
2015-03-05 22:25:26 +07:00
}
}