fake_token_exchanger.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. Copyright © The ESO Authors
  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. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package iam
  14. import (
  15. "context"
  16. "fmt"
  17. "sync/atomic"
  18. "time"
  19. )
  20. // FakeTokenExchanger simulates the process of exchanging credentials to obtain IAM tokens.
  21. // Calls keeps track of how many times the token exchange method has been invoked.
  22. // ReturnError, when set to true, forces the token exchange method to return an error.
  23. type FakeTokenExchanger struct {
  24. Calls atomic.Int64
  25. ReturnError bool
  26. }
  27. // ExchangeIamToken exchanges credentials to generate a new IAM token with a fixed 100-second validity period.
  28. func (f *FakeTokenExchanger) ExchangeIamToken(_ context.Context, _, _ string, issuedAt time.Time, _ []byte) (*Token, error) {
  29. f.Calls.Add(1)
  30. if f.ReturnError {
  31. return nil, fmt.Errorf("fake error")
  32. }
  33. return &Token{
  34. Token: fmt.Sprintf("token-%d", f.Calls.Load()),
  35. ExpiresAt: issuedAt.Add(100 * time.Second), // lifetime is 100 seconds
  36. IssuedAt: issuedAt,
  37. }, nil
  38. }