Refactor verifyClient, implement refresh tokens.
Refactor verifyClient into verifyClient and getClientAuth. We moved verifyClient
out of each of the GrantType's validation functions and into the access token
endpoint, where it will be called before the GrantType's validation function.
Yay, less code repetition. And seeing as we always want to verify the client,
that seems like a good way to prevent things like 118a69954621 from happening.
This did, however, force us to add an AllowsPublic property to the GrantType, so
the token endpoint knows whether or not a public Client is valid for any given
GrantType.
We also implemented the refresh token grant type, which required adding ClientID
and RefreshRevoked as properties on the Token type. We need ClientID because we
need to constrain refresh tokens to the client that issued them. We also should
probably keep track of which tokens belong to which clients, just as a general
rule of thumb. RefreshRevoked had to be created, next to Revoked, because the
AccessToken could be revoked and the RefreshToken still valid, or vice versa.
Notably, when you issue a new refresh token, the old one is revoked, but the
access token is still valid. It remains to be seen whether this is a good way to
track things or not. The number of duplicated properties lead me to believe our
type is not a great representation of the underlying concepts.
7 "code.secondbit.org/uuid.hg"
10 var sessionStores = []sessionStore{NewMemstore()}
12 func compareSessions(session1, session2 Session) (success bool, field string, val1, val2 interface{}) {
13 if session1.ID != session2.ID {
14 return false, "ID", session1.ID, session2.ID
16 if session1.IP != session2.IP {
17 return false, "IP", session1.IP, session2.IP
19 if session1.UserAgent != session2.UserAgent {
20 return false, "UserAgent", session1.UserAgent, session2.UserAgent
22 if !session1.ProfileID.Equal(session2.ProfileID) {
23 return false, "ProfileID", session1.ProfileID, session2.ProfileID
25 if !session1.Created.Equal(session2.Created) {
26 return false, "Created", session1.Created, session2.Created
28 if session1.Login != session2.Login {
29 return false, "Login", session1.Login, session2.Login
31 if session1.Active != session2.Active {
32 return false, "Active", session1.Active, session2.Active
34 return true, "", nil, nil
37 func TestSessionStoreSuccess(t *testing.T) {
40 ID: uuid.NewID().String() + uuid.NewID().String(),
42 UserAgent: "TestRunner",
43 ProfileID: uuid.NewID(),
45 Login: "test@example.com",
48 for _, store := range sessionStores {
49 context := Context{sessions: store}
50 err := context.CreateSession(session)
52 t.Errorf("Error saving session to %T: %s", store, err)
54 err = context.CreateSession(session)
55 if err != ErrSessionAlreadyExists {
56 t.Errorf("Expected ErrSessionAlreadyExists from %T, got %s", store, err)
58 retrieved, err := context.GetSession(session.ID)
60 t.Errorf("Error retrieving session from %T: %s", store, err)
62 success, field, expectation, result := compareSessions(session, retrieved)
64 t.Errorf("Expected field %s to be %v, but got %v from %T", field, expectation, result, store)
66 retrievedList, err := context.ListSessions(session.ProfileID, time.Time{}, 10)
68 t.Errorf("Error retrieving sessions by profile from %T: %s", store, err)
70 if len(retrievedList) != 1 {
71 t.Errorf("Expected 1 session retrieved by profile from %T, got %d", store, len(retrievedList))
73 success, field, expectation, result = compareSessions(session, retrievedList[0])
75 t.Errorf("Expected field %s to be %v, but got %v from %T", field, expectation, result, store)
77 err = context.RemoveSession(session.ID)
79 t.Errorf("Error removing session from %T: %s", store, err)
81 retrieved, err = context.GetSession(session.ID)
82 if err != ErrSessionNotFound {
83 t.Errorf("Expected ErrSessionNotFound from %T, got %s", store, err)
85 retrievedList, err = context.ListSessions(session.ProfileID, time.Time{}, 10)
87 t.Errorf("Error retrieving sessions by profile from %T: %s", store, err)
89 if len(retrievedList) != 0 {
90 t.Errorf("Expected 0 sessions retrieved by profile from %T, got %d", store, len(retrievedList))
92 err = context.RemoveSession(session.ID)
93 if err != ErrSessionNotFound {
94 t.Errorf("Expected ErrSessionNotFound from %T, got %s", store, err)