Browse Source

Add an Init() function to AuthRealms

kegan/create-with-json
Kegan Dougal 8 years ago
parent
commit
1887fc303c
  1. 4
      src/github.com/matrix-org/go-neb/realms/github/github.go
  2. 46
      src/github.com/matrix-org/go-neb/realms/jira/jira.go
  3. 4
      src/github.com/matrix-org/go-neb/types/types.go

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

@ -53,6 +53,10 @@ func (r *githubRealm) Type() string {
return "github"
}
func (r *githubRealm) Init() error {
return nil
}
func (r *githubRealm) Register() error {
return nil
}

46
src/github.com/matrix-org/go-neb/realms/jira/jira.go

@ -71,6 +71,22 @@ func (r *JIRARealm) Type() string {
return "jira"
}
// Init initialises the private key for this JIRA realm.
func (r *JIRARealm) Init() error {
if err := r.parsePrivateKey(); err != nil {
log.WithError(err).Print("Failed to parse private key")
return err
}
// Parse the messy input URL into a canonicalised form.
ju, err := urls.ParseJIRAURL(r.JIRAEndpoint)
if err != nil {
log.WithError(err).Print("Failed to parse JIRA endpoint")
return err
}
r.JIRAEndpoint = ju.Base
return nil
}
// Register is called when this realm is being created from an external entity
func (r *JIRARealm) Register() error {
if r.ConsumerName == "" || r.ConsumerKey == "" || r.ConsumerSecret == "" || r.PrivateKeyPEM == "" {
@ -80,10 +96,6 @@ func (r *JIRARealm) Register() error {
return errors.New("JIRAEndpoint must be specified")
}
if err := r.ensureInited(); err != nil {
return err
}
// Check to see if JIRA endpoint is valid by pinging an endpoint
cli, err := r.JIRAClient("", true)
if err != nil {
@ -107,10 +119,6 @@ func (r *JIRARealm) Register() error {
// RequestAuthSession is called by a user wishing to auth with this JIRA realm
func (r *JIRARealm) RequestAuthSession(userID string, req json.RawMessage) interface{} {
logger := log.WithField("jira_url", r.JIRAEndpoint)
if err := r.ensureInited(); err != nil {
logger.WithError(err).Print("Failed to init realm")
return nil
}
authConfig := r.oauth1Config(r.JIRAEndpoint)
reqToken, reqSec, err := authConfig.RequestToken()
if err != nil {
@ -143,10 +151,6 @@ func (r *JIRARealm) RequestAuthSession(userID string, req json.RawMessage) inter
// OnReceiveRedirect is called when JIRA installations redirect back to NEB
func (r *JIRARealm) OnReceiveRedirect(w http.ResponseWriter, req *http.Request) {
logger := log.WithField("jira_url", r.JIRAEndpoint)
if err := r.ensureInited(); err != nil {
failWith(logger, w, 500, "Failed to initialise realm", err)
return
}
requestToken, verifier, err := oauth1.ParseAuthorizationCallback(req)
if err != nil {
@ -203,9 +207,6 @@ func (r *JIRARealm) AuthSession(id, userID, realmID string) types.AuthSession {
// unauthenticated client will be used, which may not be able to see the complete list
// of projects.
func (r *JIRARealm) ProjectKeyExists(userID, projectKey string) (bool, error) {
if err := r.ensureInited(); err != nil {
return false, err
}
cli, err := r.JIRAClient(userID, true)
if err != nil {
return false, err
@ -274,21 +275,6 @@ func (r *JIRARealm) JIRAClient(userID string, allowUnauth bool) (*jira.Client, e
return jira.NewClient(httpClient, r.JIRAEndpoint)
}
func (r *JIRARealm) ensureInited() error {
if err := r.parsePrivateKey(); err != nil {
log.WithError(err).Print("Failed to parse private key")
return err
}
// Parse the messy input URL into a canonicalised form.
ju, err := urls.ParseJIRAURL(r.JIRAEndpoint)
if err != nil {
log.WithError(err).Print("Failed to parse JIRA endpoint")
return err
}
r.JIRAEndpoint = ju.Base
return nil
}
func (r *JIRARealm) parsePrivateKey() error {
if r.privateKey != nil {
return nil

4
src/github.com/matrix-org/go-neb/types/types.go

@ -85,6 +85,7 @@ func CreateService(serviceID, serviceType string, serviceJSON []byte) (Service,
type AuthRealm interface {
ID() string
Type() string
Init() error
Register() error
OnReceiveRedirect(w http.ResponseWriter, req *http.Request)
AuthSession(id, userID, realmID string) AuthSession
@ -110,6 +111,9 @@ func CreateAuthRealm(realmID, realmType string, realmJSON []byte) (AuthRealm, er
if err := json.Unmarshal(realmJSON, r); err != nil {
return nil, err
}
if err := r.Init(); err != nil {
return nil, err
}
return r, nil
}

Loading…
Cancel
Save