tokenman.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 mcclient
  15. import (
  16. "github.com/golang-plus/uuid"
  17. "yunion.io/x/log"
  18. )
  19. type TokenManager interface {
  20. Save(token TokenCredential) string
  21. Get(tid string) TokenCredential
  22. Remove(tid string)
  23. }
  24. type mapTokenManager struct {
  25. table map[string]TokenCredential
  26. }
  27. func (this *mapTokenManager) Save(token TokenCredential) string {
  28. key, e := uuid.NewV4()
  29. if e != nil {
  30. log.Fatalf("uuid.NewV4 returns error!")
  31. }
  32. kkey := key.String()
  33. this.table[kkey] = token
  34. // log.Println("###### Save tid", kkey)
  35. return kkey
  36. }
  37. func (this *mapTokenManager) Get(tid string) TokenCredential {
  38. // log.Println("###### Get tid", tid)
  39. return this.table[tid]
  40. }
  41. func (this *mapTokenManager) Remove(tid string) {
  42. // log.Println("###### Remove tid", tid)
  43. delete(this.table, tid)
  44. }
  45. func NewMapTokenManager() TokenManager {
  46. return &mapTokenManager{table: make(map[string]TokenCredential)}
  47. }