AuthenticationService.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // Copyright 2016, Sander van Harmelen
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. package cloudstack
  17. import (
  18. "encoding/json"
  19. "net/url"
  20. "strconv"
  21. )
  22. type LoginParams struct {
  23. p map[string]interface{}
  24. }
  25. func (p *LoginParams) toURLValues() url.Values {
  26. u := url.Values{}
  27. if p.p == nil {
  28. return u
  29. }
  30. if v, found := p.p["domain"]; found {
  31. u.Set("domain", v.(string))
  32. }
  33. if v, found := p.p["domainId"]; found {
  34. vv := strconv.FormatInt(v.(int64), 10)
  35. u.Set("domainId", vv)
  36. }
  37. if v, found := p.p["password"]; found {
  38. u.Set("password", v.(string))
  39. }
  40. if v, found := p.p["username"]; found {
  41. u.Set("username", v.(string))
  42. }
  43. return u
  44. }
  45. func (p *LoginParams) SetDomain(v string) {
  46. if p.p == nil {
  47. p.p = make(map[string]interface{})
  48. }
  49. p.p["domain"] = v
  50. return
  51. }
  52. func (p *LoginParams) SetDomainId(v int64) {
  53. if p.p == nil {
  54. p.p = make(map[string]interface{})
  55. }
  56. p.p["domainId"] = v
  57. return
  58. }
  59. func (p *LoginParams) SetPassword(v string) {
  60. if p.p == nil {
  61. p.p = make(map[string]interface{})
  62. }
  63. p.p["password"] = v
  64. return
  65. }
  66. func (p *LoginParams) SetUsername(v string) {
  67. if p.p == nil {
  68. p.p = make(map[string]interface{})
  69. }
  70. p.p["username"] = v
  71. return
  72. }
  73. // You should always use this function to get a new LoginParams instance,
  74. // as then you are sure you have configured all required params
  75. func (s *AuthenticationService) NewLoginParams(password string, username string) *LoginParams {
  76. p := &LoginParams{}
  77. p.p = make(map[string]interface{})
  78. p.p["password"] = password
  79. p.p["username"] = username
  80. return p
  81. }
  82. // Logs a user into the CloudStack. A successful login attempt will generate a JSESSIONID cookie value that can be passed in subsequent Query command calls until the "logout" command has been issued or the session has expired.
  83. func (s *AuthenticationService) Login(p *LoginParams) (*LoginResponse, error) {
  84. resp, err := s.cs.newRequest("login", p.toURLValues())
  85. if err != nil {
  86. return nil, err
  87. }
  88. var r LoginResponse
  89. if err := json.Unmarshal(resp, &r); err != nil {
  90. return nil, err
  91. }
  92. return &r, nil
  93. }
  94. type LoginResponse struct {
  95. Account string `json:"account,omitempty"`
  96. Domainid string `json:"domainid,omitempty"`
  97. Firstname string `json:"firstname,omitempty"`
  98. Lastname string `json:"lastname,omitempty"`
  99. Registered string `json:"registered,omitempty"`
  100. Sessionkey string `json:"sessionkey,omitempty"`
  101. Timeout int `json:"timeout,omitempty"`
  102. Timezone string `json:"timezone,omitempty"`
  103. Type string `json:"type,omitempty"`
  104. Userid string `json:"userid,omitempty"`
  105. Username string `json:"username,omitempty"`
  106. }
  107. type LogoutParams struct {
  108. p map[string]interface{}
  109. }
  110. func (p *LogoutParams) toURLValues() url.Values {
  111. u := url.Values{}
  112. if p.p == nil {
  113. return u
  114. }
  115. return u
  116. }
  117. // You should always use this function to get a new LogoutParams instance,
  118. // as then you are sure you have configured all required params
  119. func (s *AuthenticationService) NewLogoutParams() *LogoutParams {
  120. p := &LogoutParams{}
  121. p.p = make(map[string]interface{})
  122. return p
  123. }
  124. // Logs out the user
  125. func (s *AuthenticationService) Logout(p *LogoutParams) (*LogoutResponse, error) {
  126. resp, err := s.cs.newRequest("logout", p.toURLValues())
  127. if err != nil {
  128. return nil, err
  129. }
  130. var r LogoutResponse
  131. if err := json.Unmarshal(resp, &r); err != nil {
  132. return nil, err
  133. }
  134. return &r, nil
  135. }
  136. type LogoutResponse struct {
  137. Description string `json:"description,omitempty"`
  138. }