Last active
January 8, 2024 22:23
-
-
Save ewollesen/4d4778b6b9ef65b87fd3ab4b07887159 to your computer and use it in GitHub Desktop.
A small utility for swapping the hosts from keycloak SSO metadata
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module saml-replace | |
go 1.21.5 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
// saml-replace is a small utility for generating a SAML service provider | |
// metadata URL to provide to a clinic that's integrating their SSO. | |
// | |
// git clone [email protected]:4d4778b6b9ef65b87fd3ab4b07887159.git saml-replace | |
// | |
// $ ./saml-replace \ | |
// --keycloak-url <URL where you downloaded the SAML metadata (XML)> \ | |
// --xml-filename <filename where you saved the SAML metadata XML> | |
import ( | |
"encoding/xml" | |
"flag" | |
"fmt" | |
"log" | |
"net/url" | |
"os" | |
) | |
type samlMetadata struct { | |
EntityID string `xml:"entityID,attr"` | |
} | |
func main() { | |
var keycloakURL, xmlFilename string | |
flag.StringVar(&keycloakURL, "keycloak-url", "", "the keycloak URL with the metadata XML") | |
flag.StringVar(&xmlFilename, "xml-filename", "-", "the XML metadata from keycloak") | |
flag.Parse() | |
var f *os.File = os.Stdin | |
if xmlFilename != "-" { | |
xmlFile, err := os.Open(xmlFilename) | |
if err != nil { | |
log.Fatalf("opening XML filename %q: %s", xmlFilename, err) | |
} | |
defer xmlFile.Close() | |
f = xmlFile | |
} | |
metadata := &samlMetadata{} | |
if err := xml.NewDecoder(f).Decode(&metadata); err != nil { | |
log.Fatal("parsing XML: %s", err) | |
} | |
entityURL, err := url.Parse(metadata.EntityID) | |
if err != nil { | |
log.Fatalf("parsing entityID URL: %s", err) | |
} | |
kcURL, err := url.Parse(keycloakURL) | |
if err != nil { | |
log.Fatalf("parsing keycloak-url: %s", err) | |
} | |
kcURL.Host = entityURL.Host | |
fmt.Println(kcURL.String()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment