Browse Source

Make StarterLink a property of the AuthRealm

pull/27/head
Kegan Dougal 8 years ago
parent
commit
44abae4977
  1. 19
      src/github.com/matrix-org/go-neb/realms/github/github.go
  2. 11
      src/github.com/matrix-org/go-neb/services/github/github.go

19
src/github.com/matrix-org/go-neb/realms/github/github.go

@ -12,11 +12,12 @@ import (
"net/url" "net/url"
) )
type githubRealm struct {
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
@ -45,23 +46,23 @@ func (s *GithubSession) ID() string {
return s.id return s.id
} }
func (r *githubRealm) ID() string {
func (r *GithubRealm) ID() string {
return r.id return r.id
} }
func (r *githubRealm) Type() string {
func (r *GithubRealm) Type() string {
return "github" return "github"
} }
func (r *githubRealm) Init() error {
func (r *GithubRealm) Init() error {
return nil return nil
} }
func (r *githubRealm) Register() error {
func (r *GithubRealm) Register() error {
return nil return nil
} }
func (r *githubRealm) RequestAuthSession(userID string, req json.RawMessage) interface{} {
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 +91,7 @@ func (r *githubRealm) RequestAuthSession(userID string, req json.RawMessage) int
}{u.String()} }{u.String()}
} }
func (r *githubRealm) OnReceiveRedirect(w http.ResponseWriter, req *http.Request) {
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 +149,7 @@ 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 {
func (r *GithubRealm) AuthSession(id, userID, realmID string) types.AuthSession {
return &GithubSession{ return &GithubSession{
id: id, id: id,
userID: userID, userID: userID,
@ -175,6 +176,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}
}) })
} }

11
src/github.com/matrix-org/go-neb/services/github/github.go

@ -28,7 +28,6 @@ type githubService struct {
ClientUserID string ClientUserID string
RealmID string RealmID string
SecretToken string SecretToken string
StarterLink string
Rooms map[string]struct { // room_id => {} Rooms map[string]struct { // room_id => {}
Repos map[string]struct { // owner/repo => { events: ["push","issue","pull_request"] } Repos map[string]struct { // owner/repo => { events: ["push","issue","pull_request"] }
Events []string Events []string
@ -50,9 +49,17 @@ func (s *githubService) RoomIDs() []string {
func (s *githubService) cmdGithubCreate(roomID, userID string, args []string) (interface{}, error) { func (s *githubService) cmdGithubCreate(roomID, userID string, args []string) (interface{}, error) {
cli := s.githubClientFor(userID, false) cli := s.githubClientFor(userID, false)
if cli == nil { if cli == nil {
r, err := database.GetServiceDB().LoadAuthRealm(s.RealmID)
if err != nil {
return nil, err
}
ghRealm, ok := r.(*realms.GithubRealm)
if !ok {
return nil, fmt.Errorf("Failed to cast realm %s into a GithubRealm", s.RealmID)
}
return matrix.StarterLinkMessage{ return matrix.StarterLinkMessage{
Body: "You need to OAuth with Github before you can create issues.", Body: "You need to OAuth with Github before you can create issues.",
Link: s.StarterLink,
Link: ghRealm.StarterLink,
}, nil }, nil
} }

Loading…
Cancel
Save