auth clean up and redirect fixes
This commit is contained in:
@@ -24,6 +24,7 @@ import (
|
||||
type (
|
||||
webpageRouter struct {
|
||||
log *logging.Logger
|
||||
uiPath string
|
||||
contentDir string
|
||||
templater *templater.Templater
|
||||
rawEvents *raw_events.Store
|
||||
@@ -41,6 +42,7 @@ type (
|
||||
func SetupRoutes(
|
||||
logger *logging.Logger,
|
||||
r gin.IRoutes,
|
||||
uiPath string,
|
||||
contentDir string,
|
||||
rawEvents *raw_events.Store,
|
||||
accts *accounts.Store,
|
||||
@@ -50,6 +52,7 @@ func SetupRoutes(
|
||||
|
||||
s := &webpageRouter{
|
||||
log: logger,
|
||||
uiPath: uiPath,
|
||||
contentDir: contentDir,
|
||||
// TODO: clean up the references to the templates dir throughout as well (should only be referred to in the 'main' package
|
||||
templater: new(templater.Templater).With(templater.Config{
|
||||
@@ -101,6 +104,12 @@ func SetupRoutes(
|
||||
// GET /
|
||||
// compiles the page template or component template matching the url
|
||||
func (s *webpageRouter) serveTemplate(c *gin.Context) (response.Response, error) {
|
||||
// trim the url path prefix before loading the templates
|
||||
c.Request.URL.Path = c.Request.URL.Path[len(s.uiPath):]
|
||||
defer func() {
|
||||
c.Request.URL.Path = s.uiPath + c.Request.URL.Path
|
||||
}()
|
||||
|
||||
r := c.Request
|
||||
ctx := r.Context()
|
||||
|
||||
@@ -222,6 +231,7 @@ func authorizeByMatchingAccountID(r *http.Request, acctIDPathPosition int) error
|
||||
return nil
|
||||
}
|
||||
|
||||
// TODO: is this error type even needed anymore?
|
||||
func (e ErrTemplateNotFound) Error() string {
|
||||
if e.err != nil {
|
||||
return fmt.Sprintf("template not found: %v", e.err)
|
||||
|
||||
@@ -22,7 +22,6 @@ type (
|
||||
Auth struct {
|
||||
log *logging.Logger
|
||||
auth *authentication.Authenticator
|
||||
newLoginURL LoginURLProviderFunc
|
||||
accts *accounts.Store
|
||||
}
|
||||
|
||||
@@ -41,26 +40,24 @@ type (
|
||||
func NewAuth(
|
||||
logger *logging.Logger,
|
||||
auth *authentication.Authenticator,
|
||||
newLoginURL LoginURLProviderFunc,
|
||||
accts *accounts.Store,
|
||||
) *Auth {
|
||||
return &Auth{
|
||||
log: logger,
|
||||
auth: auth,
|
||||
newLoginURL: newLoginURL,
|
||||
accts: accts,
|
||||
}
|
||||
}
|
||||
|
||||
// AddIdentityToRequest will add an Identity to the context that can then be retrieved via GetIdentity.
|
||||
func (a *Auth) AddIdentityToRequest(c *gin.Context) {
|
||||
if err := a.addIdentityToRequest(c); err != nil {
|
||||
// AddIdentity will add an Identity to the context that can then be retrieved via GetIdentity.
|
||||
func (a *Auth) AddIdentity(c *gin.Context) {
|
||||
if err := a.addIdentity(c); err != nil {
|
||||
c.Error(err)
|
||||
c.Abort()
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Auth) addIdentityToRequest(c *gin.Context) error {
|
||||
func (a *Auth) addIdentity(c *gin.Context) error {
|
||||
r := c.Request
|
||||
|
||||
ck, err := r.Cookie("access_token")
|
||||
@@ -101,7 +98,7 @@ func (a *Auth) addIdentityToRequest(c *gin.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Authenticate should only be used along with and after AddIdentityToRequest
|
||||
// Authenticate should only be used along with and after AddIdentity
|
||||
// Typically used with response.Handler to make a gin.HandlerFunc.
|
||||
func (a *Auth) Authenticate(assertions ...AuthorizationAssertions) func(c *gin.Context) (response.Response, error) {
|
||||
return func(c *gin.Context) (response.Response, error) {
|
||||
|
||||
@@ -81,7 +81,8 @@ func writeResponse(c *gin.Context, res Response) {
|
||||
}
|
||||
|
||||
if code, to, ok := res.GetRedirect(); ok {
|
||||
http.Redirect(w, c.Request, to, code.Int())
|
||||
c.Redirect(code.Int(), to)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ func NewRouter(
|
||||
authMiddleware := middleware.NewAuth(
|
||||
logger.WithGroup("auth-middleware"),
|
||||
auth,
|
||||
auth_api.NewLoginURL,
|
||||
//auth_api.NewLoginURL,
|
||||
accts,
|
||||
)
|
||||
|
||||
@@ -81,9 +81,9 @@ func NewRouter(
|
||||
logger.WithGroup("templates"),
|
||||
r.Group(
|
||||
"/ui",
|
||||
stripPrefix("/ui"),
|
||||
authMiddleware.AddIdentityToRequest,
|
||||
authMiddleware.AddIdentity,
|
||||
),
|
||||
"/ui",
|
||||
contentDir,
|
||||
rawEvents,
|
||||
accts,
|
||||
@@ -113,7 +113,7 @@ func NewRouter(
|
||||
sse_api.Routes(
|
||||
api.Group(
|
||||
"/events",
|
||||
authMiddleware.AddIdentityToRequest,
|
||||
authMiddleware.AddIdentity,
|
||||
response.Handler(authMiddleware.Authenticate()),
|
||||
),
|
||||
apiLogger.WithGroup("/events"),
|
||||
@@ -122,7 +122,7 @@ func NewRouter(
|
||||
accounts_api.Routes(
|
||||
api.Group(
|
||||
"/accounts",
|
||||
authMiddleware.AddIdentityToRequest,
|
||||
authMiddleware.AddIdentity,
|
||||
response.Handler(authMiddleware.Authenticate()),
|
||||
),
|
||||
apiLogger.WithGroup("/accounts"),
|
||||
@@ -165,22 +165,5 @@ func fileServer(urlPrefix, dir string, beforeServe func(c *gin.Context)) gin.Han
|
||||
scfs.ServeHTTP(c.Writer, r)
|
||||
c.Abort()
|
||||
}
|
||||
|
||||
// TODO: need a c.Next()?
|
||||
}
|
||||
}
|
||||
|
||||
func stripPrefix(prefix string) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
if !strings.HasPrefix(c.Request.URL.Path, prefix) {
|
||||
return
|
||||
}
|
||||
|
||||
c.Request.URL.Path = c.Request.URL.Path[len(prefix):]
|
||||
defer func() {
|
||||
c.Request.URL.Path = prefix + c.Request.URL.Path
|
||||
}()
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user