auth.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright (c) 2016 VMware, Inc. All Rights Reserved.
  2. //
  3. // This product is licensed to you under the Apache License, Version 2.0 (the "License").
  4. // You may not use this product except in compliance with the License.
  5. //
  6. // This product may include a number of subcomponents with separate copyright notices and
  7. // license terms. Your use of these subcomponents is subject to the terms and conditions
  8. // of the subcomponent's license, as noted in the LICENSE file.
  9. package photon
  10. import (
  11. "encoding/json"
  12. "fmt"
  13. "github.com/vmware/photon-controller-go-sdk/photon/lightwave"
  14. )
  15. // Contains functionality for auth API.
  16. type AuthAPI struct {
  17. client *Client
  18. }
  19. const authUrl string = rootUrl + "/auth"
  20. // Gets authentication info.
  21. func (api *AuthAPI) Get() (info *AuthInfo, err error) {
  22. res, err := api.client.restClient.Get(api.client.Endpoint+authUrl, nil)
  23. if err != nil {
  24. return
  25. }
  26. defer res.Body.Close()
  27. res, err = getError(res)
  28. if err != nil {
  29. return
  30. }
  31. info = &AuthInfo{}
  32. err = json.NewDecoder(res.Body).Decode(info)
  33. return
  34. }
  35. // Gets Tokens from username/password.
  36. func (api *AuthAPI) GetTokensByPassword(username string, password string) (tokenOptions *TokenOptions, err error) {
  37. oidcClient, err := api.buildOIDCClient()
  38. if err != nil {
  39. return
  40. }
  41. tokenResponse, err := oidcClient.GetTokenByPasswordGrant(username, password)
  42. if err != nil {
  43. return
  44. }
  45. return api.toTokenOptions(tokenResponse), nil
  46. }
  47. // GetTokensFromWindowsLogInContext gets tokens based on Windows logged in context
  48. // In case of running on platform other than Windows, it returns error
  49. func (api *AuthAPI) GetTokensFromWindowsLogInContext() (tokenOptions *TokenOptions, err error) {
  50. oidcClient, err := api.buildOIDCClient()
  51. if err != nil {
  52. return
  53. }
  54. tokenResponse, err := oidcClient.GetTokensFromWindowsLogInContext()
  55. if err != nil {
  56. return
  57. }
  58. return api.toTokenOptions(tokenResponse), nil
  59. }
  60. // Gets tokens from refresh token.
  61. func (api *AuthAPI) GetTokensByRefreshToken(refreshtoken string) (tokenOptions *TokenOptions, err error) {
  62. oidcClient, err := api.buildOIDCClient()
  63. if err != nil {
  64. return
  65. }
  66. tokenResponse, err := oidcClient.GetTokenByRefreshTokenGrant(refreshtoken)
  67. if err != nil {
  68. return
  69. }
  70. return api.toTokenOptions(tokenResponse), nil
  71. }
  72. func (api *AuthAPI) getAuthEndpoint() (endpoint string, err error) {
  73. authInfo, err := api.client.Auth.Get()
  74. if err != nil {
  75. return
  76. }
  77. if authInfo.Port == 0 {
  78. authInfo.Port = 443
  79. }
  80. return fmt.Sprintf("https://%s:%d", authInfo.Endpoint, authInfo.Port), nil
  81. }
  82. func (api *AuthAPI) buildOIDCClient() (client *lightwave.OIDCClient, err error) {
  83. authEndPoint, err := api.getAuthEndpoint()
  84. if err != nil {
  85. return
  86. }
  87. return lightwave.NewOIDCClient(
  88. authEndPoint,
  89. api.buildOIDCClientOptions(&api.client.options),
  90. api.client.restClient.logger), nil
  91. }
  92. const tokenScope string = "openid offline_access rs_photon_platform at_groups"
  93. func (api *AuthAPI) buildOIDCClientOptions(options *ClientOptions) *lightwave.OIDCClientOptions {
  94. return &lightwave.OIDCClientOptions{
  95. IgnoreCertificate: api.client.options.IgnoreCertificate,
  96. RootCAs: api.client.options.RootCAs,
  97. TokenScope: tokenScope,
  98. }
  99. }
  100. func (api *AuthAPI) toTokenOptions(response *lightwave.OIDCTokenResponse) *TokenOptions {
  101. return &TokenOptions{
  102. AccessToken: response.AccessToken,
  103. ExpiresIn: response.ExpiresIn,
  104. RefreshToken: response.RefreshToken,
  105. IdToken: response.IdToken,
  106. TokenType: response.TokenType,
  107. }
  108. }
  109. // Parse the given token details.
  110. func (api *AuthAPI) parseTokenDetails(token string) (jwtToken *lightwave.JWTToken, err error) {
  111. jwtToken = lightwave.ParseTokenDetails(token)
  112. return jwtToken, nil
  113. }
  114. // Parse the given token raw details.
  115. func (api *AuthAPI) parseRawTokenDetails(token string) (jwtToken []string, err error) {
  116. jwtToken, err = lightwave.ParseRawTokenDetails(token)
  117. return jwtToken, err
  118. }