1
0
Fork 0
empact/server/auth.go

44 lines
999 B
Go
Raw Normal View History

2015-03-05 07:46:19 +00:00
package server
import (
"fmt"
"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/db"
"github.com/localhots/empact/job"
"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)
params.Set("scope", "repo")
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")))
} else {
2015-03-05 12:57:36 +00:00
code := r.FormValue("code")
fmt.Println("Got code: ", code)
res := make(chan string)
job.Enqueue(&task.FetchAccessTokenTask{
Code: code,
Result: res,
Task: &db.Task{},
})
if token, ok := <-res; ok {
fmt.Println("Got access token: ", token)
w.Write([]byte(token))
} else {
panic("Failed to fetch token")
}
2015-03-05 07:46:19 +00:00
}
}