Skip to content

Commit

Permalink
Merge pull request #12 from portward/rename-token-service
Browse files Browse the repository at this point in the history
feat!: rename token to authz service
  • Loading branch information
sagikazarmark authored Sep 25, 2023
2 parents b46f642 + d263938 commit 214c2e5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 27 deletions.
12 changes: 6 additions & 6 deletions auth/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ func init() {
decoder.IgnoreUnknownKeys(true)
}

// TokenServer implements the [Docker Registry v2 authentication] specification.
// AuthorizationServer implements the [Docker Registry v2 authentication] specification.
//
// [Docker Registry v2 authentication]: https://github.com/distribution/distribution/blob/main/docs/spec/auth/index.md
type TokenServer struct {
Service TokenService
type AuthorizationServer struct {
Service AuthorizationService

ErrorHandler ErrorHandler
}
Expand All @@ -36,7 +36,7 @@ func httpHandleError(err error, w http.ResponseWriter) {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}

func (s TokenServer) handleError(err error) {
func (s AuthorizationServer) handleError(err error) {
if s.ErrorHandler == nil {
return
}
Expand All @@ -47,7 +47,7 @@ func (s TokenServer) handleError(err error) {
// TokenHandler implements the [Docker Registry v2 authentication] specification.
//
// [Docker Registry v2 authentication]: https://github.com/distribution/distribution/blob/main/docs/spec/auth/token.md
func (s TokenServer) TokenHandler(w http.ResponseWriter, r *http.Request) {
func (s AuthorizationServer) TokenHandler(w http.ResponseWriter, r *http.Request) {
request, err := decodeTokenRequest(r)
if err != nil {
s.handleError(fmt.Errorf("decoding token request: %w", err))
Expand Down Expand Up @@ -108,7 +108,7 @@ type rawTokenRequest struct {
// OAuth2Handler implements the [Docker Registry v2 OAuth2 authentication] specification.
//
// [Docker Registry v2 OAuth2 authentication]: https://github.com/distribution/distribution/blob/main/docs/spec/auth/oauth.md
func (s TokenServer) OAuth2Handler(w http.ResponseWriter, r *http.Request) {
func (s AuthorizationServer) OAuth2Handler(w http.ResponseWriter, r *http.Request) {
request, err := decodeOAuth2Request(r)
if err != nil {
s.handleError(fmt.Errorf("decoding oauth2 token request: %w", err))
Expand Down
6 changes: 3 additions & 3 deletions auth/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (g idGeneratorStub) GenerateID() (string, error) {
return g.id, nil
}

func TestServer(t *testing.T) {
func TestAuthorizationServer(t *testing.T) {
t.Parallel()

const (
Expand Down Expand Up @@ -99,13 +99,13 @@ func TestServer(t *testing.T) {

authorizer := authz.NewDefaultAuthorizer(authz.NewDefaultRepositoryAuthorizer(true), true)

service := auth.TokenServiceImpl{
service := auth.AuthorizationServiceImpl{
Authenticator: authenticator,
Authorizer: authorizer,
TokenIssuer: tokenIssuer,
}

server := auth.TokenServer{
server := auth.AuthorizationServer{
Service: service,
}

Expand Down
35 changes: 17 additions & 18 deletions auth/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ import (
"time"
)

// TokenService implements both the [Docker Registry v2 authentication] and the [Docker Registry v2 OAuth2 authentication] specification.
// AuthorizationService defines an interface for the [Docker Registry v2 authentication].
//
// [Docker Registry v2 authentication]: https://github.com/distribution/distribution/blob/main/docs/spec/auth/token.md
// [Docker Registry v2 OAuth2 authentication]: https://github.com/distribution/distribution/blob/main/docs/spec/auth/oauth.md
type TokenService interface {
// [Docker Registry v2 authentication]: https://github.com/distribution/distribution/blob/main/docs/spec/auth/index.md
type AuthorizationService interface {
// TokenHandler implements the [Docker Registry v2 authentication] specification.
//
// [Docker Registry v2 authentication]: https://github.com/distribution/distribution/blob/main/docs/spec/auth/token.md
Expand Down Expand Up @@ -146,31 +145,31 @@ type OAuth2Response struct {
RefreshToken string `json:"refresh_token,omitempty"`
}

// Authenticator is a facade combining different type of authenticators.
// AuthorizationServiceImpl implements the [Docker Registry v2 authentication] specification.
//
// [Docker Registry v2 authentication]: https://github.com/distribution/distribution/blob/main/docs/spec/auth/index.md
type AuthorizationServiceImpl struct {
Authenticator Authenticator
Authorizer Authorizer
TokenIssuer TokenIssuer
}

// Authenticator is a facade combining a [PasswordAuthenticator] and a [RefreshTokenAuthenticator].
type Authenticator struct {
PasswordAuthenticator
RefreshTokenAuthenticator
}

// TokenIssuer is a facade combining different type of token issuers.
// TokenIssuer is a facade combining an [AccessTokenIssuer] and a [RefreshTokenIssuer].
type TokenIssuer struct {
AccessTokenIssuer
RefreshTokenIssuer
}

// TokenServer implements the [Docker Registry v2 authentication] specification.
//
// [Docker Registry v2 authentication]: https://github.com/distribution/distribution/blob/main/docs/spec/auth/index.md
type TokenServiceImpl struct {
Authenticator Authenticator
Authorizer Authorizer
TokenIssuer TokenIssuer
}

// TokenHandler implements the [Docker Registry v2 authentication] specification.
//
// [Docker Registry v2 authentication]: https://github.com/distribution/distribution/blob/main/docs/spec/auth/token.md
func (s TokenServiceImpl) TokenHandler(ctx context.Context, r TokenRequest) (TokenResponse, error) {
func (s AuthorizationServiceImpl) TokenHandler(ctx context.Context, r TokenRequest) (TokenResponse, error) {
if err := r.Validate(); err != nil {
return TokenResponse{}, err
}
Expand Down Expand Up @@ -213,7 +212,7 @@ func (s TokenServiceImpl) TokenHandler(ctx context.Context, r TokenRequest) (Tok
return response, nil
}

func (s TokenServiceImpl) OAuth2Handler(ctx context.Context, r OAuth2Request) (OAuth2Response, error) {
func (s AuthorizationServiceImpl) OAuth2Handler(ctx context.Context, r OAuth2Request) (OAuth2Response, error) {
if err := r.Validate(); err != nil {
return OAuth2Response{}, err
}
Expand Down Expand Up @@ -278,7 +277,7 @@ func (s TokenServiceImpl) OAuth2Handler(ctx context.Context, r OAuth2Request) (O

// LoggerTokenService acts as a middleware for a TokenService and logs every request.
type LoggerTokenService struct {
Service TokenService
Service AuthorizationService
Logger *slog.Logger
}

Expand Down

0 comments on commit 214c2e5

Please sign in to comment.