Add option to use public repos only.

This commit is contained in:
Mihai Parparita 2014-10-17 21:06:55 -07:00
parent 9a43bfc5bb
commit c285bc0e02
3 changed files with 36 additions and 8 deletions

View file

@ -27,6 +27,7 @@ import (
var router *mux.Router
var githubOauthConfig oauth.Config
var githubOauthPublicConfig oauth.Config
var timezones Timezones
var sessionStore *sessions.CookieStore
var sessionConfig SessionConfig
@ -36,7 +37,8 @@ func init() {
initTemplates()
timezones = initTimezones()
sessionStore, sessionConfig = initSession()
initGithubOAuthConfig()
githubOauthConfig = initGithubOAuthConfig(true)
githubOauthPublicConfig = initGithubOAuthConfig(false)
router = mux.NewRouter()
router.HandleFunc("/", indexHandler).Name("index")
@ -58,7 +60,7 @@ func init() {
http.Handle("/", router)
}
func initGithubOAuthConfig() {
func initGithubOAuthConfig(includePrivateRepos bool) (config oauth.Config) {
path := "config/github-oauth"
if appengine.IsDevAppServer() {
path += "-dev"
@ -68,13 +70,18 @@ func initGithubOAuthConfig() {
if err != nil {
log.Panicf("Could not read GitHub OAuth config from %s: %s", path, err.Error())
}
err = json.Unmarshal(configBytes, &githubOauthConfig)
err = json.Unmarshal(configBytes, &config)
if err != nil {
log.Panicf("Could not parse GitHub OAuth config %s: %s", configBytes, err.Error())
}
githubOauthConfig.Scope = "repo, user:email"
githubOauthConfig.AuthURL = "https://github.com/login/oauth/authorize"
githubOauthConfig.TokenURL = "https://github.com/login/oauth/access_token"
repoScopeModifier := ""
if !includePrivateRepos {
repoScopeModifier = "public_"
}
config.Scope = fmt.Sprintf("%srepo user:email", repoScopeModifier)
config.AuthURL = "https://github.com/login/oauth/authorize"
config.TokenURL = "https://github.com/login/oauth/access_token"
return
}
func initTemplates() {
@ -156,7 +163,11 @@ func loadStyles() (result map[string]template.CSS) {
}
func signInHandler(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, githubOauthConfig.AuthCodeURL(""), http.StatusFound)
config := &githubOauthConfig
if r.FormValue("include_private") != "1" {
config = &githubOauthPublicConfig
}
http.Redirect(w, r, config.AuthCodeURL(""), http.StatusFound)
}
func signOutHandler(w http.ResponseWriter, r *http.Request) {

View file

@ -8,6 +8,19 @@ a {
text-decoration: none;
}
#sign-in-form {
margin: 0 auto;
}
#sign-in-form input[type="submit"] {
font-size: 18pt;
font-weight: bold;
}
#sign-in-form label {
display: block;
}
#primary-actions,
#primary-actions input[type="submit"] {
font-size: 14pt;

View file

@ -2,8 +2,12 @@
{{define "body"}}
<form method="POST" action="{{routeUrl "sign-in"}}">
<form id="sign-in-form" method="POST" action="{{routeUrl "sign-in"}}">
<input type="submit" value="Sign In with GitHub">
<label>
<input type="checkbox" name="include_private" value="1" checked>
Include private repositories
</label>
</form>
{{end}}