-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add OAuth token and user validator interface
This change adds the OAuthTokenValidator interface for generically validating an OAuthAccessToken and User. The expiration and UID validation was pulled out from the tokenAuthenticator. tokenAuthenticator simply takes OAuthTokenValidators as input, and delegates validation to them. This allows all future validation to simply append itself to the list of validators without requiring any changes to tokenAuthenticator. Signed-off-by: Monis Khan <[email protected]>
- Loading branch information
Showing
6 changed files
with
147 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package registry | ||
|
||
import ( | ||
"errors" | ||
"time" | ||
|
||
"github.com/openshift/origin/pkg/auth/authenticator" | ||
"github.com/openshift/origin/pkg/oauth/apis/oauth" | ||
"github.com/openshift/origin/pkg/user/apis/user" | ||
) | ||
|
||
var errExpired = errors.New("token is expired") | ||
|
||
func NewExpirationValidator() authenticator.OAuthTokenValidator { | ||
return authenticator.OAuthTokenValidatorFunc( | ||
func(token *oauth.OAuthAccessToken, _ *user.User) error { | ||
if token.ExpiresIn > 0 { | ||
if expire(token).Before(time.Now()) { | ||
return errExpired | ||
} | ||
} | ||
if token.DeletionTimestamp != nil { | ||
return errExpired | ||
} | ||
return nil | ||
}, | ||
) | ||
} | ||
|
||
func expire(token *oauth.OAuthAccessToken) time.Time { | ||
return token.CreationTimestamp.Add(time.Duration(token.ExpiresIn) * time.Second) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package registry | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/openshift/origin/pkg/auth/authenticator" | ||
"github.com/openshift/origin/pkg/oauth/apis/oauth" | ||
userapi "github.com/openshift/origin/pkg/user/apis/user" | ||
) | ||
|
||
const errInvalidUIDStr = "user.UID (%s) does not match token.userUID (%s)" | ||
|
||
func NewUIDValidator() authenticator.OAuthTokenValidator { | ||
return authenticator.OAuthTokenValidatorFunc( | ||
func(token *oauth.OAuthAccessToken, user *userapi.User) error { | ||
if string(user.UID) != token.UserUID { | ||
return fmt.Errorf(errInvalidUIDStr, user.UID, token.UserUID) | ||
} | ||
return nil | ||
}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters