|
@ -12,11 +12,13 @@ import ( |
|
|
"net/url" |
|
|
"net/url" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
type githubRealm struct { |
|
|
|
|
|
|
|
|
// GithubRealm can handle OAuth processes with github.com
|
|
|
|
|
|
type GithubRealm struct { |
|
|
id string |
|
|
id string |
|
|
redirectURL string |
|
|
redirectURL string |
|
|
ClientSecret string |
|
|
ClientSecret string |
|
|
ClientID string |
|
|
ClientID string |
|
|
|
|
|
StarterLink string |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// GithubSession represents an authenticated github session
|
|
|
// GithubSession represents an authenticated github session
|
|
@ -30,6 +32,11 @@ type GithubSession struct { |
|
|
realmID string |
|
|
realmID string |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Authenticated returns true if the user has completed the auth process
|
|
|
|
|
|
func (s *GithubSession) Authenticated() bool { |
|
|
|
|
|
return s.AccessToken != "" |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// UserID returns the user_id who authorised with Github
|
|
|
// UserID returns the user_id who authorised with Github
|
|
|
func (s *GithubSession) UserID() string { |
|
|
func (s *GithubSession) UserID() string { |
|
|
return s.userID |
|
|
return s.userID |
|
@ -45,23 +52,28 @@ func (s *GithubSession) ID() string { |
|
|
return s.id |
|
|
return s.id |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (r *githubRealm) ID() string { |
|
|
|
|
|
|
|
|
// ID returns the realm ID
|
|
|
|
|
|
func (r *GithubRealm) ID() string { |
|
|
return r.id |
|
|
return r.id |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (r *githubRealm) Type() string { |
|
|
|
|
|
|
|
|
// Type is github
|
|
|
|
|
|
func (r *GithubRealm) Type() string { |
|
|
return "github" |
|
|
return "github" |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (r *githubRealm) Init() error { |
|
|
|
|
|
|
|
|
// Init does nothing.
|
|
|
|
|
|
func (r *GithubRealm) Init() error { |
|
|
return nil |
|
|
return nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (r *githubRealm) Register() error { |
|
|
|
|
|
|
|
|
// Register does nothing.
|
|
|
|
|
|
func (r *GithubRealm) Register() error { |
|
|
return nil |
|
|
return nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (r *githubRealm) RequestAuthSession(userID string, req json.RawMessage) interface{} { |
|
|
|
|
|
|
|
|
// RequestAuthSession generates an OAuth2 URL for this user to auth with github via.
|
|
|
|
|
|
func (r *GithubRealm) RequestAuthSession(userID string, req json.RawMessage) interface{} { |
|
|
state, err := randomString(10) |
|
|
state, err := randomString(10) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
log.WithError(err).Print("Failed to generate state param") |
|
|
log.WithError(err).Print("Failed to generate state param") |
|
@ -90,7 +102,8 @@ func (r *githubRealm) RequestAuthSession(userID string, req json.RawMessage) int |
|
|
}{u.String()} |
|
|
}{u.String()} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (r *githubRealm) OnReceiveRedirect(w http.ResponseWriter, req *http.Request) { |
|
|
|
|
|
|
|
|
// OnReceiveRedirect processes OAuth redirect requests from Github
|
|
|
|
|
|
func (r *GithubRealm) OnReceiveRedirect(w http.ResponseWriter, req *http.Request) { |
|
|
// parse out params from the request
|
|
|
// parse out params from the request
|
|
|
code := req.URL.Query().Get("code") |
|
|
code := req.URL.Query().Get("code") |
|
|
state := req.URL.Query().Get("state") |
|
|
state := req.URL.Query().Get("state") |
|
@ -148,7 +161,8 @@ func (r *githubRealm) OnReceiveRedirect(w http.ResponseWriter, req *http.Request |
|
|
w.Write([]byte("OK!")) |
|
|
w.Write([]byte("OK!")) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (r *githubRealm) AuthSession(id, userID, realmID string) types.AuthSession { |
|
|
|
|
|
|
|
|
// AuthSession returns a GithubSession for this user
|
|
|
|
|
|
func (r *GithubRealm) AuthSession(id, userID, realmID string) types.AuthSession { |
|
|
return &GithubSession{ |
|
|
return &GithubSession{ |
|
|
id: id, |
|
|
id: id, |
|
|
userID: userID, |
|
|
userID: userID, |
|
@ -175,6 +189,6 @@ func randomString(length int) (string, error) { |
|
|
|
|
|
|
|
|
func init() { |
|
|
func init() { |
|
|
types.RegisterAuthRealm(func(realmID, redirectURL string) types.AuthRealm { |
|
|
types.RegisterAuthRealm(func(realmID, redirectURL string) types.AuthRealm { |
|
|
return &githubRealm{id: realmID, redirectURL: redirectURL} |
|
|
|
|
|
|
|
|
return &GithubRealm{id: realmID, redirectURL: redirectURL} |
|
|
}) |
|
|
}) |
|
|
} |
|
|
} |