Test our Postgres profileStore implementation.
Update all our test cases to use time.Now().Round(time.Millisecond), because Go
uses nanosecond precision on time values, but Postgres silently truncates that
to millisecond precision. This caused our tests to report false failures that
were just silent precision loss, not actual failures.
Set up our authd server to use the Postgres store for profiles and automatically
create a test scope when starting up.
Log errors when creating Clients through the API, instead of just swallowing
them and sending back cryptic act of god errors.
Add a NewPostgres helper that returns a postgres profileStore from a connection
string (passed through pq transparently).
Add an Empty() bool helper to ProfileChange and BulkProfileChange types, so we
can determine if there are any changes we need to act on easily.
Log errors when creating Pofiles through the API, instead of just swalloing them
and sending back cryptic act of god errors.
Remove the ` quotes around field and table names, which are not supported in
Postgres. This required adding a few functions/methods to pan.
Detect situations where a profile was expected and not found, and return
ErrProfileNotFound.
Detect pq errors thrown when the profiles_pkey constraint is violated, and
transform them to the ErrProfileAlreadyExists error.
Detect empty ProfileChange and BulkProfileChange variables and abort the
updateProfile and updateProfiles methods early, before invalid SQL is generated.
Detect pq errors thrown when the logins_pkey constraint is violated, and
transform them to the ErrLoginAlreadyExists error.
Detect when removing a Login and no rows were affected, and return an
ErrLoginNotFound.
Create an sql dir with a postgres_init script that will initialize the schema of
the tables expected in the database.
9 "code.secondbit.org/uuid.hg"
13 RegisterGrantType("authorization_code", GrantType{
14 Validate: authCodeGrantValidate,
15 Invalidate: authCodeGrantInvalidate,
17 ReturnToken: RenderJSONToken,
19 AuditString: authCodeGrantAuditString,
24 // ErrNoAuthorizationCodeStore is returned when a Context tries to act on a authorizationCodeStore without setting one first.
25 ErrNoAuthorizationCodeStore = errors.New("no authorizationCodeStore was specified for the Context")
26 // ErrAuthorizationCodeNotFound is returned when an AuthorizationCode is requested but not found in the authorizationCodeStore.
27 ErrAuthorizationCodeNotFound = errors.New("authorization code not found in authorizationCodeStore")
28 // ErrAuthorizationCodeAlreadyExists is returned when an AuthorizationCode is added to a authorizationCodeStore, but another AuthorizationCode with the
29 // same Code already exists in the authorizationCodeStore.
30 ErrAuthorizationCodeAlreadyExists = errors.New("authorization code already exists in authorizationCodeStore")
33 // AuthorizationCode represents an authorization grant made by a user to a Client, to
34 // access user data within a defined Scope for a limited amount of time.
35 type AuthorizationCode struct {
47 type authorizationCodeStore interface {
48 getAuthorizationCode(code string) (AuthorizationCode, error)
49 saveAuthorizationCode(authCode AuthorizationCode) error
50 deleteAuthorizationCode(code string) error
51 useAuthorizationCode(code string) error
54 func (m *memstore) getAuthorizationCode(code string) (AuthorizationCode, error) {
55 m.authCodeLock.RLock()
56 defer m.authCodeLock.RUnlock()
57 authCode, ok := m.authCodes[code]
59 return AuthorizationCode{}, ErrAuthorizationCodeNotFound
64 func (m *memstore) saveAuthorizationCode(authCode AuthorizationCode) error {
66 defer m.authCodeLock.Unlock()
67 _, ok := m.authCodes[authCode.Code]
69 return ErrAuthorizationCodeAlreadyExists
71 m.authCodes[authCode.Code] = authCode
75 func (m *memstore) deleteAuthorizationCode(code string) error {
77 defer m.authCodeLock.Unlock()
78 _, ok := m.authCodes[code]
80 return ErrAuthorizationCodeNotFound
82 delete(m.authCodes, code)
86 func (m *memstore) useAuthorizationCode(code string) error {
88 defer m.authCodeLock.Unlock()
89 a, ok := m.authCodes[code]
91 return ErrAuthorizationCodeNotFound
98 func authCodeGrantValidate(w http.ResponseWriter, r *http.Request, context Context) (scopes []string, profileID uuid.ID, valid bool) {
99 enc := json.NewEncoder(w)
100 code := r.PostFormValue("code")
102 w.WriteHeader(http.StatusBadRequest)
103 renderJSONError(enc, "invalid_request")
106 clientID, _, ok := getClientAuth(w, r, true)
110 authCode, err := context.GetAuthorizationCode(code)
112 if err == ErrAuthorizationCodeNotFound {
113 w.WriteHeader(http.StatusBadRequest)
114 renderJSONError(enc, "invalid_grant")
117 w.WriteHeader(http.StatusInternalServerError)
118 renderJSONError(enc, "server_error")
121 redirectURI := r.PostFormValue("redirect_uri")
122 if authCode.RedirectURI != redirectURI {
123 w.WriteHeader(http.StatusBadRequest)
124 renderJSONError(enc, "invalid_grant")
127 if !authCode.ClientID.Equal(clientID) {
128 w.WriteHeader(http.StatusBadRequest)
129 renderJSONError(enc, "invalid_grant")
132 return authCode.Scopes, authCode.ProfileID, true
135 func authCodeGrantInvalidate(r *http.Request, context Context) error {
136 code := r.PostFormValue("code")
138 return ErrAuthorizationCodeNotFound
140 return context.UseAuthorizationCode(code)
143 func authCodeGrantAuditString(r *http.Request) string {
144 return "authcode:" + r.PostFormValue("code")