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