Skip to content

Commit

Permalink
consent: Populate consent session with default values (#989)
Browse files Browse the repository at this point in the history
This resolves a panic when the session is not available. Closes #988

Signed-off-by: arekkas <aeneas@ory.am>
  • Loading branch information
arekkas authored Aug 16, 2018
1 parent 96f4cb3 commit c67b7fe
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 30 deletions.
12 changes: 12 additions & 0 deletions consent/strategy_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,18 @@ func (s *DefaultStrategy) verifyConsent(w http.ResponseWriter, r *http.Request,
return nil, err
}

if session.Session == nil {
session.Session = newConsentRequestSessionData()
}

if session.Session.AccessToken == nil {
session.Session.AccessToken = map[string]interface{}{}
}

if session.Session.IDToken == nil {
session.Session.IDToken = map[string]interface{}{}
}

session.ConsentRequest.SubjectIdentifier = pw
session.AuthenticatedAt = session.ConsentRequest.AuthenticatedAt
return session, nil
Expand Down
50 changes: 20 additions & 30 deletions consent/strategy_default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ package consent
import (
"crypto/rand"
"crypto/rsa"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"net/http/httptest"
"net/url"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -540,17 +542,19 @@ func TestStrategy(t *testing.T) {
require.EqualValues(t, http.StatusOK, res.StatusCode)
assert.False(t, rr.Skip)

v, res, err := apiClient.AcceptConsentRequest(r.URL.Query().Get("consent_challenge"), swagger.AcceptConsentRequest{
GrantScope: []string{"scope-a"},
Remember: true,
RememberFor: 0,
Session: swagger.ConsentRequestSession{
AccessToken: map[string]interface{}{"foo": "bar"},
IdToken: map[string]interface{}{"bar": "baz"},
},
})
body := `{"grant_scope": ["scope-a"], "remember": true}`
require.NoError(t, err)
require.EqualValues(t, http.StatusOK, res.StatusCode)
req, err := http.NewRequest("PUT", api.URL+"/oauth2/auth/requests/consent/"+r.URL.Query().Get("consent_challenge")+"/accept", strings.NewReader(body))
req.Header.Add("Content-Type", "application/json")
require.NoError(t, err)

hres, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer hres.Body.Close()

var v swagger.CompletedRequest
require.NoError(t, json.NewDecoder(hres.Body).Decode(&v))
require.EqualValues(t, http.StatusOK, hres.StatusCode)
require.NotEmpty(t, v.RedirectTo)
http.Redirect(w, r, v.RedirectTo, http.StatusFound)
}
Expand All @@ -563,10 +567,7 @@ func TestStrategy(t *testing.T) {
GrantedScope: []string{"scope-a"},
Remember: true,
RememberFor: 0,
Session: &ConsentRequestSessionData{
AccessToken: map[string]interface{}{"foo": "bar"},
IDToken: map[string]interface{}{"bar": "baz"},
},
Session: newConsentRequestSessionData(),
},
},
{
Expand Down Expand Up @@ -940,10 +941,7 @@ func TestStrategy(t *testing.T) {
GrantedScope: []string{"scope-a"},
Remember: false,
RememberFor: 0,
Session: &ConsentRequestSessionData{
AccessToken: map[string]interface{}{"foo": "bar"},
IDToken: map[string]interface{}{"bar": "baz"},
},
Session: newConsentRequestSessionData(),
},
}, // these tests depend on one another
{
Expand Down Expand Up @@ -980,10 +978,7 @@ func TestStrategy(t *testing.T) {
GrantedScope: []string{"scope-a"},
Remember: false,
RememberFor: 0,
Session: &ConsentRequestSessionData{
AccessToken: map[string]interface{}{"foo": "bar"},
IDToken: map[string]interface{}{"bar": "baz"},
},
Session: newConsentRequestSessionData(),
},
},
{
Expand Down Expand Up @@ -1017,10 +1012,7 @@ func TestStrategy(t *testing.T) {
GrantedScope: []string{"scope-a"},
Remember: false,
RememberFor: 0,
Session: &ConsentRequestSessionData{
AccessToken: map[string]interface{}{"foo": "bar"},
IDToken: map[string]interface{}{"bar": "baz"},
},
Session: newConsentRequestSessionData(),
},
}, // these tests depend on one another
{
Expand Down Expand Up @@ -1055,10 +1047,7 @@ func TestStrategy(t *testing.T) {
GrantedScope: []string{"scope-a"},
Remember: false,
RememberFor: 0,
Session: &ConsentRequestSessionData{
AccessToken: map[string]interface{}{"foo": "bar"},
IDToken: map[string]interface{}{"bar": "baz"},
},
Session: newConsentRequestSessionData(),
},
},

Expand Down Expand Up @@ -1239,6 +1228,7 @@ func TestStrategy(t *testing.T) {
require.NotNil(t, c)
assert.EqualValues(t, tc.expectSession.GrantedScope, c.GrantedScope)
assert.EqualValues(t, tc.expectSession.Remember, c.Remember)
assert.EqualValues(t, tc.expectSession.Session, c.Session)
assert.EqualValues(t, tc.expectSession.RememberFor, c.RememberFor)
assert.EqualValues(t, tc.expectSession.ConsentRequest.Subject, c.ConsentRequest.Subject)
assert.EqualValues(t, tc.expectSession.ConsentRequest.SubjectIdentifier, c.ConsentRequest.SubjectIdentifier)
Expand Down
7 changes: 7 additions & 0 deletions consent/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,10 @@ type ConsentRequestSessionData struct {

//UserInfo map[string]interface{} `json:"userinfo"`
}

func newConsentRequestSessionData() *ConsentRequestSessionData {
return &ConsentRequestSessionData{
AccessToken: map[string]interface{}{},
IDToken: map[string]interface{}{},
}
}

0 comments on commit c67b7fe

Please sign in to comment.