Browse Source

Add redirect URL for JIRA auth

pull/33/head
Kegan Dougal 8 years ago
parent
commit
233e85eeee
  1. 1
      README.md
  2. 25
      src/github.com/matrix-org/go-neb/realms/jira/jira.go

1
README.md

@ -266,6 +266,7 @@ curl -X POST localhost:4050/admin/requestAuthSession --data-binary '{
"RealmID": "jirarealm",
"UserID": "@example:localhost",
"Config": {
"RedirectURL": "https://optional-url.com/to/redirect/to/after/auth"
}
}'
```

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

@ -45,6 +45,7 @@ type JIRASession struct {
RequestSecret string
AccessToken string
AccessSecret string
ClientsRedirectURL string // where to redirect the client to after auth
}
// Authenticated returns true if the user has completed the auth process
@ -132,6 +133,16 @@ 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)
// check if they supplied a redirect URL
var reqBody struct {
RedirectURL string
}
if err := json.Unmarshal(req, &reqBody); err != nil {
log.WithError(err).Print("Failed to decode request body")
return nil
}
authConfig := r.oauth1Config(r.JIRAEndpoint)
reqToken, reqSec, err := authConfig.RequestToken()
if err != nil {
@ -150,6 +161,7 @@ func (r *JIRARealm) RequestAuthSession(userID string, req json.RawMessage) inter
userID: userID,
realmID: r.id,
RequestSecret: reqSec,
ClientsRedirectURL: reqBody.RedirectURL,
})
if err != nil {
log.WithError(err).Print("Failed to store new auth session")
@ -202,8 +214,19 @@ func (r *JIRARealm) OnReceiveRedirect(w http.ResponseWriter, req *http.Request)
failWith(logger, w, 500, "Failed to persist JIRA session", err)
return
}
if jiraSession.ClientsRedirectURL != "" {
w.WriteHeader(302)
w.Header().Set("Location", jiraSession.ClientsRedirectURL)
// technically don't need a body but *shrug*
w.Write([]byte(jiraSession.ClientsRedirectURL))
} else {
w.WriteHeader(200)
w.Write([]byte("OK!"))
w.Write([]byte(
fmt.Sprintf("You have successfully linked your JIRA account on %s to %s",
r.JIRAEndpoint, jiraSession.UserID(),
),
))
}
}
// AuthSession returns a JIRASession with the given parameters

Loading…
Cancel
Save