authorizer/server/handlers/app.go

87 lines
2.6 KiB
Go
Raw Normal View History

package handlers
import (
"net/http"
"strings"
2022-05-23 06:22:51 +00:00
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"github.com/authorizerdev/authorizer/server/constants"
2022-05-29 11:52:46 +00:00
"github.com/authorizerdev/authorizer/server/memorystore"
2022-05-30 06:24:16 +00:00
"github.com/authorizerdev/authorizer/server/parsers"
"github.com/authorizerdev/authorizer/server/validators"
)
2022-01-17 06:02:13 +00:00
// State is the struct that holds authorizer url and redirect url
// They are provided via query string in the request
type State struct {
AuthorizerURL string `json:"authorizerURL"`
RedirectURL string `json:"redirectURL"`
}
2022-01-17 06:02:13 +00:00
// AppHandler is the handler for the /app route
func AppHandler() gin.HandlerFunc {
return func(c *gin.Context) {
2022-05-30 06:24:16 +00:00
hostname := parsers.GetHost(c)
2022-05-29 11:52:46 +00:00
if isLoginPageDisabled, err := memorystore.Provider.GetBoolStoreEnvVariable(constants.EnvKeyDisableLoginPage); err != nil || isLoginPageDisabled {
2022-05-23 06:22:51 +00:00
log.Debug("Login page is disabled")
2022-01-25 07:36:52 +00:00
c.JSON(400, gin.H{"error": "login page is not enabled"})
return
}
2022-10-21 05:49:32 +00:00
redirectURI := strings.TrimSpace(c.Query("redirect_uri"))
state := strings.TrimSpace(c.Query("state"))
scopeString := strings.TrimSpace(c.Query("scope"))
var scope []string
if scopeString == "" {
scope = []string{"openid", "profile", "email"}
} else {
scope = strings.Split(scopeString, " ")
}
2022-10-21 05:49:32 +00:00
if redirectURI == "" {
redirectURI = hostname + "/app"
} else {
// validate redirect url with allowed origins
2022-10-21 05:49:32 +00:00
if !validators.IsValidOrigin(redirectURI) {
2022-05-23 06:22:51 +00:00
log.Debug("Invalid redirect_uri")
c.JSON(400, gin.H{"error": "invalid redirect url"})
return
}
}
// debug the request state
if pusher := c.Writer.Pusher(); pusher != nil {
// use pusher.Push() to do server push
if err := pusher.Push("/app/build/bundle.js", nil); err != nil {
2022-05-25 07:00:22 +00:00
log.Debug("Failed to push file path: ", err)
}
}
2022-05-29 11:52:46 +00:00
orgName, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyOrganizationName)
if err != nil {
log.Debug("Failed to get organization name")
c.JSON(400, gin.H{"error": "failed to get organization name"})
return
}
orgLogo, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyOrganizationLogo)
if err != nil {
log.Debug("Failed to get organization logo")
c.JSON(400, gin.H{"error": "failed to get organization logo"})
return
}
c.HTML(http.StatusOK, "app.tmpl", gin.H{
"data": map[string]interface{}{
"authorizerURL": hostname,
2022-10-21 05:49:32 +00:00
"redirectURL": redirectURI,
"scope": scope,
"state": state,
2022-05-29 11:52:46 +00:00
"organizationName": orgName,
"organizationLogo": orgLogo,
},
})
}
}