Skip to content

Commit 64901c4

Browse files
Merge pull request #540 from USACE/feature/444-identity-provider-endpoint
add keycloak endpoint based on env
2 parents 3e21f8c + ae5c783 commit 64901c4

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

api/handlers/helpers.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package handlers
22

33
import (
44
"errors"
5+
"net/http"
6+
"strings"
57

68
"github.com/USACE/cumulus-api/api/middleware"
79
"github.com/google/uuid"
@@ -15,3 +17,51 @@ func GetSub(c echo.Context) (*uuid.UUID, error) {
1517
}
1618
return userInfo.Sub, nil
1719
}
20+
21+
// GetIdentityProviderConfiguration returns the Keycloak configuration based on the auth environment and realm
22+
func GetIdentityProviderConfiguration(authEnv string, c echo.Context) error {
23+
24+
// Convert the authEnv to lowercase to make the comparison case-insensitive
25+
authEnv = strings.ToLower(authEnv)
26+
27+
// Set the base URL for Keycloak depending on the environment
28+
var keycloakHost string
29+
realm := "cwbi" // Set the realm as a variable
30+
31+
// Determine the Keycloak host based on the authEnv passed
32+
switch authEnv {
33+
// ----------------------------
34+
// Satisfy local mocking
35+
case "mock":
36+
keycloakHost = "http://localhost"
37+
// ----------------------------
38+
// Castle Cloud auth servers
39+
case "develop":
40+
keycloakHost = "https://develop-auth.corps.cloud"
41+
realm = "water"
42+
case "stable":
43+
keycloakHost = "https://auth.corps.cloud"
44+
realm = "water"
45+
// ----------------------------
46+
// CWBI auth servers
47+
case "dev":
48+
keycloakHost = "https://identityc-test.cwbi.us"
49+
case "test":
50+
keycloakHost = "https://identityc-test.cwbi.us"
51+
case "prod":
52+
keycloakHost = "https://identityc.sec.usace.army.mil"
53+
default:
54+
return c.JSON(http.StatusBadRequest, map[string]string{
55+
"error": "Invalid auth environment: " + authEnv,
56+
})
57+
}
58+
59+
// Prepare the configuration as a map of string keys and values
60+
config := map[string]string{
61+
"token_endpoint": keycloakHost + "/auth/realms/" + realm + "/protocol/openid-connect/token",
62+
"well_known_endpoint": keycloakHost + "/auth/realms/" + realm + "/.well-known/openid-configuration",
63+
}
64+
65+
// Return the configuration as a JSON response
66+
return c.JSON(http.StatusOK, config)
67+
}

api/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ func main() {
114114
})
115115
})
116116

117+
// Identity Provider Configuration Route
118+
public.GET("/identity-provider/configuration", func(c echo.Context) error {
119+
return handlers.GetIdentityProviderConfiguration(cfg.AuthEnvironment, c)
120+
})
121+
117122
// Proxy to pg_featureserv
118123
features := e.Group("/features")
119124
features.Use(middleware.PgFeatureservProxy(cfg.PgFeatureservUrl))

0 commit comments

Comments
 (0)