instance.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. "crypto/rsa"
  17. "crypto/x509"
  18. "yunion.io/x/pkg/errors"
  19. )
  20. type SSAMLInstance struct {
  21. entityID string
  22. privateKeyFile string
  23. certFile string
  24. certString string
  25. privateKey *rsa.PrivateKey
  26. certs []*x509.Certificate
  27. }
  28. func NewSAMLInstance(entityID string, cert, key string) (*SSAMLInstance, error) {
  29. saml := SSAMLInstance{
  30. privateKeyFile: key,
  31. certFile: cert,
  32. entityID: entityID,
  33. }
  34. err := saml.parseKeys()
  35. if err != nil {
  36. return nil, errors.Wrap(err, "saml.parseKeys")
  37. }
  38. return &saml, nil
  39. }
  40. func (saml *SSAMLInstance) GetEntityId() string {
  41. return saml.entityID
  42. }
  43. func (saml *SSAMLInstance) GetCertString() string {
  44. return "\n" + saml.certString + "\n"
  45. }
  46. func (saml *SSAMLInstance) SignXML(xmlstr string) (string, error) {
  47. return SignXML(xmlstr, saml.privateKey)
  48. }
  49. func (saml *SSAMLInstance) SetEntityId(id string) {
  50. saml.entityID = id
  51. }