Skip to content

Instantly share code, notes, and snippets.

@ewollesen
Last active January 8, 2024 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ewollesen/4d4778b6b9ef65b87fd3ab4b07887159 to your computer and use it in GitHub Desktop.
Save ewollesen/4d4778b6b9ef65b87fd3ab4b07887159 to your computer and use it in GitHub Desktop.
A small utility for swapping the hosts from keycloak SSO metadata
module saml-replace
go 1.21.5
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 git@gist.github.com: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