util.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package samlutils
  15. import (
  16. "bytes"
  17. "compress/flate"
  18. "crypto/rsa"
  19. "encoding/base64"
  20. "fmt"
  21. "io"
  22. "strings"
  23. "github.com/ma314smith/signedxml"
  24. "yunion.io/x/log"
  25. "yunion.io/x/pkg/errors"
  26. "yunion.io/x/pkg/utils"
  27. )
  28. func compressString(in string) string {
  29. buf := new(bytes.Buffer)
  30. compressor, _ := flate.NewWriter(buf, 9)
  31. compressor.Write([]byte(in))
  32. compressor.Close()
  33. return buf.String()
  34. }
  35. func decompressString(in string) string {
  36. buf := new(bytes.Buffer)
  37. decompressor := flate.NewReader(strings.NewReader(in))
  38. io.Copy(buf, decompressor)
  39. decompressor.Close()
  40. return buf.String()
  41. }
  42. func compress(in []byte) ([]byte, error) {
  43. buf := new(bytes.Buffer)
  44. compressor, _ := flate.NewWriter(buf, 9)
  45. _, err := compressor.Write(in)
  46. if err != nil {
  47. return nil, errors.Wrap(err, "compressor.Write")
  48. }
  49. compressor.Close()
  50. return buf.Bytes(), nil
  51. }
  52. func decompress(in []byte) ([]byte, error) {
  53. buf := new(bytes.Buffer)
  54. decompressor := flate.NewReader(bytes.NewReader(in))
  55. _, err := io.Copy(buf, decompressor)
  56. if err != nil {
  57. return nil, errors.Wrap(err, "io.Copy")
  58. }
  59. decompressor.Close()
  60. return buf.Bytes(), nil
  61. }
  62. func SAMLDecode(input string) ([]byte, error) {
  63. reqBytes, err := base64.StdEncoding.DecodeString(input)
  64. if err != nil {
  65. return nil, errors.Wrap(err, "base64.StdEncoding.DecodeString")
  66. }
  67. return func() []byte {
  68. // Azure no need to decompress
  69. plainText, err := decompress(reqBytes)
  70. if err != nil {
  71. log.Warningf("decompress %s error: %v", string(reqBytes), err)
  72. return reqBytes
  73. }
  74. return plainText
  75. }(), nil
  76. }
  77. func SAMLEncode(input []byte) (string, error) {
  78. comp, err := compress(input)
  79. if err != nil {
  80. return "", errors.Wrap(err, "compress")
  81. }
  82. return base64.StdEncoding.EncodeToString(comp), nil
  83. }
  84. func SAMLForm(action string, attrs map[string]string) string {
  85. form := strings.Builder{}
  86. // form.WriteString(`<!DOCTYPE html><html lang="en-US"><body>`)
  87. form.WriteString(`<form id="saml_submit_form" method="POST" action="`)
  88. form.WriteString(action)
  89. form.WriteString(`">`)
  90. for k, v := range attrs {
  91. form.WriteString(fmt.Sprintf("<input type=\"hidden\" name=\"%s\" value=\"%s\" />", k, v))
  92. }
  93. form.WriteString(`<input type="submit" value="Submit" />`)
  94. form.WriteString("</form><script><!--\n")
  95. form.WriteString("document.getElementById('saml_submit_form').submit();\n")
  96. form.WriteString(`//--></script>`)
  97. // form.WriteString(`</body></html>`)
  98. return form.String()
  99. }
  100. func SignXML(xmlstr string, privateKey *rsa.PrivateKey) (string, error) {
  101. signer, err := signedxml.NewSigner(string(xmlstr))
  102. if err != nil {
  103. return "", errors.Wrap(err, "signedxml.NewSigner")
  104. }
  105. signed, err := signer.Sign(privateKey)
  106. if err != nil {
  107. return "", errors.Wrap(err, "signer.Sign")
  108. }
  109. return signed, nil
  110. }
  111. func ValidateXML(signed string) ([]string, error) {
  112. validator, err := signedxml.NewValidator(signed)
  113. if err != nil {
  114. return nil, errors.Wrap(err, "signedxml.NewValidator")
  115. }
  116. validXMLs, err := validator.ValidateReferences()
  117. if err != nil {
  118. return nil, errors.Wrap(err, "validator.ValidateReferences")
  119. }
  120. return validXMLs, nil
  121. }
  122. func GenerateSAMLId() string {
  123. return "_" + utils.GenRequestId(16)
  124. }