@@ -2,6 +2,8 @@ package handlers
22
33import (
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+ }
0 commit comments