authmethods.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 tokens
  15. import (
  16. api "yunion.io/x/onecloud/pkg/apis/identity"
  17. )
  18. func authMethodStr2Id(method string) byte {
  19. for i := range api.AUTH_METHODS {
  20. if api.AUTH_METHODS[i] == method {
  21. return byte(i) + 1
  22. }
  23. }
  24. return 0
  25. }
  26. func authMethodId2Str(mid byte) string {
  27. if mid >= 1 && int(mid) <= len(api.AUTH_METHODS) {
  28. return api.AUTH_METHODS[mid-1]
  29. }
  30. return ""
  31. }
  32. func authMethodsStr2Id(methods []string) []byte {
  33. ret := make([]byte, len(methods))
  34. for i := range methods {
  35. ret[i] = authMethodStr2Id(methods[i])
  36. }
  37. return ret
  38. }
  39. func authMethodsId2Str(mids []byte) []string {
  40. ret := make([]string, len(mids))
  41. for i := range mids {
  42. ret[i] = authMethodId2Str(mids[i])
  43. }
  44. return ret
  45. }