2022-05-30 11:54:16 +05:30
|
|
|
package parsers
|
2021-07-15 00:13:19 +05:30
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
2021-08-04 12:18:57 +05:30
|
|
|
"strings"
|
2022-01-31 11:35:24 +05:30
|
|
|
|
2022-06-05 22:13:10 +05:30
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
2022-03-15 08:53:48 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/constants"
|
2022-05-30 09:19:55 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/memorystore"
|
2021-07-15 00:13:19 +05:30
|
|
|
)
|
|
|
|
|
2022-01-31 11:35:24 +05:30
|
|
|
// GetHost returns hostname from request context
|
2022-09-28 10:36:56 +05:30
|
|
|
// if EnvKeyAuthorizerURL is set it is given highest priority.
|
|
|
|
// if X-Authorizer-URL header is set it is given second highest priority
|
2022-03-30 11:50:22 +05:30
|
|
|
// if above 2 are not set the requesting host name is used
|
2022-01-31 11:35:24 +05:30
|
|
|
func GetHost(c *gin.Context) string {
|
2022-06-05 22:13:10 +05:30
|
|
|
authorizerURL, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAuthorizerURL)
|
|
|
|
if err != nil {
|
|
|
|
authorizerURL = ""
|
|
|
|
}
|
2022-03-30 11:50:22 +05:30
|
|
|
if authorizerURL != "" {
|
2022-11-24 12:58:04 +05:30
|
|
|
return strings.TrimSuffix(authorizerURL, "/")
|
2022-03-30 11:50:22 +05:30
|
|
|
}
|
|
|
|
|
2022-06-05 22:13:10 +05:30
|
|
|
authorizerURL = c.Request.Header.Get("X-Authorizer-URL")
|
2022-03-30 11:50:22 +05:30
|
|
|
if authorizerURL != "" {
|
2022-11-24 12:58:04 +05:30
|
|
|
return strings.TrimSuffix(authorizerURL, "/")
|
2022-03-30 11:50:22 +05:30
|
|
|
}
|
|
|
|
|
2022-01-31 14:30:13 +05:30
|
|
|
scheme := c.Request.Header.Get("X-Forwarded-Proto")
|
|
|
|
if scheme != "https" {
|
|
|
|
scheme = "http"
|
2022-01-31 11:35:24 +05:30
|
|
|
}
|
2022-01-31 14:30:13 +05:30
|
|
|
host := c.Request.Host
|
2022-11-24 12:58:04 +05:30
|
|
|
return strings.TrimSuffix(scheme+"://"+host, "/")
|
2022-01-31 11:35:24 +05:30
|
|
|
}
|
|
|
|
|
2021-12-21 18:46:54 +05:30
|
|
|
// GetHostName function returns hostname and port
|
|
|
|
func GetHostParts(uri string) (string, string) {
|
|
|
|
tempURI := uri
|
2022-01-23 01:24:41 +05:30
|
|
|
if !strings.HasPrefix(tempURI, "http://") && !strings.HasPrefix(tempURI, "https://") {
|
2021-12-21 18:46:54 +05:30
|
|
|
tempURI = "https://" + tempURI
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := url.Parse(tempURI)
|
2021-08-04 12:18:57 +05:30
|
|
|
if err != nil {
|
2021-12-21 18:46:54 +05:30
|
|
|
return "localhost", "8080"
|
2021-08-04 12:18:57 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
host := u.Hostname()
|
2021-12-21 18:46:54 +05:30
|
|
|
port := u.Port()
|
2021-08-04 12:18:57 +05:30
|
|
|
|
2021-12-21 18:46:54 +05:30
|
|
|
return host, port
|
2021-08-04 12:18:57 +05:30
|
|
|
}
|
|
|
|
|
2021-12-11 06:41:35 +05:30
|
|
|
// GetDomainName function to get domain name
|
2021-12-21 18:46:54 +05:30
|
|
|
func GetDomainName(uri string) string {
|
|
|
|
tempURI := uri
|
2022-01-23 01:24:41 +05:30
|
|
|
if !strings.HasPrefix(tempURI, "http://") && !strings.HasPrefix(tempURI, "https://") {
|
2021-12-21 18:46:54 +05:30
|
|
|
tempURI = "https://" + tempURI
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := url.Parse(tempURI)
|
2021-07-15 00:13:19 +05:30
|
|
|
if err != nil {
|
|
|
|
return `localhost`
|
|
|
|
}
|
|
|
|
|
2021-07-22 05:22:53 +05:30
|
|
|
host := u.Hostname()
|
|
|
|
|
2021-07-28 23:53:54 +05:30
|
|
|
// code to get root domain in case of sub-domains
|
2021-08-04 12:18:57 +05:30
|
|
|
hostParts := strings.Split(host, ".")
|
|
|
|
hostPartsLen := len(hostParts)
|
|
|
|
|
|
|
|
if hostPartsLen == 1 {
|
|
|
|
return host
|
|
|
|
}
|
|
|
|
|
|
|
|
if hostPartsLen == 2 {
|
|
|
|
if hostParts[0] == "www" {
|
|
|
|
return hostParts[1]
|
|
|
|
} else {
|
|
|
|
return host
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if hostPartsLen > 2 {
|
|
|
|
return strings.Join(hostParts[hostPartsLen-2:], ".")
|
|
|
|
}
|
2021-07-22 05:22:53 +05:30
|
|
|
|
|
|
|
return host
|
2021-07-15 00:13:19 +05:30
|
|
|
}
|
2022-03-15 08:53:48 +05:30
|
|
|
|
2022-10-19 23:55:47 +05:30
|
|
|
// GetAppURL to get /app url if not configured by user
|
2022-03-15 08:53:48 +05:30
|
|
|
func GetAppURL(gc *gin.Context) string {
|
2022-05-30 11:54:16 +05:30
|
|
|
envAppURL, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAppURL)
|
|
|
|
if envAppURL == "" || err != nil {
|
2022-03-15 09:57:09 +05:30
|
|
|
envAppURL = GetHost(gc) + "/app"
|
2022-03-15 08:53:48 +05:30
|
|
|
}
|
|
|
|
return envAppURL
|
|
|
|
}
|