-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from containerish/refactor
refactor: refactored to make code more manageable
- Loading branch information
Showing
11 changed files
with
328 additions
and
194 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,63 @@ | ||
package auth | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/containerish/OpenRegistry/registry/v2" | ||
"github.com/labstack/echo/v4" | ||
"github.com/labstack/echo/v4/middleware" | ||
) | ||
|
||
//when we use JWT | ||
/*AuthMiddleware | ||
HTTP/1.1 401 Unauthorized | ||
Content-Type: application/json; charset=utf-8 | ||
Docker-Distribution-Api-Version: registry/2.0 | ||
Www-Authenticate: Bearer realm="https://auth.docker.io/token",service="registry.docker.io", | ||
scope="repository:samalba/my-app:pull,push" | ||
Date: Thu, 10 Sep 2015 19:32:31 GMT | ||
Content-Length: 235 | ||
Strict-Transport-Security: max-age=31536000 | ||
{"errors":[{"code":"UNAUTHORIZED","message":"","detail":}]} | ||
*/ | ||
//var wwwAuthenticate = `Bearer realm="http://0.0.0.0:5000/auth/token", | ||
//service="http://0.0.0.0:5000",scope="repository:%s` | ||
|
||
// BasicAuth returns a middleware which in turn can be used to perform http basic auth | ||
func (a *auth) BasicAuth() echo.MiddlewareFunc { | ||
return middleware.BasicAuth(func(username string, password string, ctx echo.Context) (bool, error) { | ||
|
||
if ctx.Request().RequestURI != "/v2/" { | ||
if ctx.Request().Method == http.MethodHead || ctx.Request().Method == http.MethodGet { | ||
return true, nil | ||
} | ||
} | ||
|
||
if ctx.Request().RequestURI == "/v2/" { | ||
_, err := a.validateUser(username, password) | ||
if err != nil { | ||
return false, ctx.NoContent(http.StatusUnauthorized) | ||
} | ||
return true, nil | ||
} | ||
|
||
usernameFromNameSpace := ctx.Param("username") | ||
if usernameFromNameSpace != username { | ||
var errMsg registry.RegistryErrors | ||
errMsg.Errors = append(errMsg.Errors, registry.RegistryError{ | ||
Code: registry.RegistryErrorCodeDenied, | ||
Message: "not authorised", | ||
Detail: nil, | ||
}) | ||
return false, ctx.JSON(http.StatusForbidden, errMsg) | ||
} | ||
resp, err := a.validateUser(username, password) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
ctx.Set("basic_auth", resp) | ||
return true, 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
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 |
---|---|---|
@@ -1 +1,32 @@ | ||
package ratelimiter | ||
|
||
import ( | ||
"net/http" | ||
"time" | ||
|
||
"github.com/labstack/echo/v4" | ||
"github.com/labstack/echo/v4/middleware" | ||
) | ||
|
||
func New() echo.MiddlewareFunc { | ||
storeConfig := middleware.RateLimiterMemoryStoreConfig{ | ||
Rate: 3, | ||
Burst: 0, | ||
ExpiresIn: time.Hour * 10, | ||
} | ||
|
||
return middleware.RateLimiterWithConfig(middleware.RateLimiterConfig{ | ||
Skipper: middleware.DefaultSkipper, | ||
BeforeFunc: nil, | ||
IdentifierExtractor: func(ctx echo.Context) (string, error) { | ||
return ctx.RealIP(), nil | ||
}, | ||
Store: middleware.NewRateLimiterMemoryStoreWithConfig(storeConfig), | ||
ErrorHandler: func(ctx echo.Context, err error) error { | ||
return ctx.JSON(http.StatusForbidden, echo.Map{"error": "Too many requests, try after some time!"}) | ||
}, | ||
DenyHandler: func(ctx echo.Context, identifier string, err error) error { | ||
return ctx.JSON(http.StatusForbidden, echo.Map{"error": "Too many requests, try after some time!"}) | ||
}, | ||
}) | ||
} |
Oops, something went wrong.