sso.go 938 B

123456789101112131415161718192021222324252627282930313233343536
  1. package discourse
  2. import (
  3. "crypto/hmac"
  4. "crypto/sha256"
  5. "encoding/base64"
  6. "encoding/hex"
  7. "fmt"
  8. "net/url"
  9. )
  10. func generateSSORequestURL(config SsoConfig) string {
  11. // Build the query string payload
  12. payload := fmt.Sprintf("nonce=%s&return_sso_url=%s", config.Uuid, config.ReturnEndpoint)
  13. // Base64 encode the payload
  14. base64Payload := base64.StdEncoding.EncodeToString([]byte(payload))
  15. // Urlencode the
  16. URLEncodedPayload := url.QueryEscape(base64Payload)
  17. // Get a hex signature for this payload with the sso secret
  18. hexSignature := computeHmac256(base64Payload, config.Secret)
  19. ssoURL := fmt.Sprintf("%s?sso=%s&sig=%s", config.Endpoint, URLEncodedPayload, hexSignature)
  20. return ssoURL
  21. }
  22. // Create a hex signature from a message and secret
  23. func computeHmac256(message string, secret string) string {
  24. key := []byte(secret)
  25. h := hmac.New(sha256.New, key)
  26. h.Write([]byte(message))
  27. return hex.EncodeToString(h.Sum(nil))
  28. }