parse.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2023 Google LLC
  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 credsfile
  15. import (
  16. "encoding/json"
  17. )
  18. // ParseServiceAccount parses bytes into a [ServiceAccountFile].
  19. func ParseServiceAccount(b []byte) (*ServiceAccountFile, error) {
  20. var f *ServiceAccountFile
  21. if err := json.Unmarshal(b, &f); err != nil {
  22. return nil, err
  23. }
  24. return f, nil
  25. }
  26. // ParseClientCredentials parses bytes into a
  27. // [credsfile.ClientCredentialsFile].
  28. func ParseClientCredentials(b []byte) (*ClientCredentialsFile, error) {
  29. var f *ClientCredentialsFile
  30. if err := json.Unmarshal(b, &f); err != nil {
  31. return nil, err
  32. }
  33. return f, nil
  34. }
  35. // ParseUserCredentials parses bytes into a [UserCredentialsFile].
  36. func ParseUserCredentials(b []byte) (*UserCredentialsFile, error) {
  37. var f *UserCredentialsFile
  38. if err := json.Unmarshal(b, &f); err != nil {
  39. return nil, err
  40. }
  41. return f, nil
  42. }
  43. // ParseExternalAccount parses bytes into a [ExternalAccountFile].
  44. func ParseExternalAccount(b []byte) (*ExternalAccountFile, error) {
  45. var f *ExternalAccountFile
  46. if err := json.Unmarshal(b, &f); err != nil {
  47. return nil, err
  48. }
  49. return f, nil
  50. }
  51. // ParseExternalAccountAuthorizedUser parses bytes into a
  52. // [ExternalAccountAuthorizedUserFile].
  53. func ParseExternalAccountAuthorizedUser(b []byte) (*ExternalAccountAuthorizedUserFile, error) {
  54. var f *ExternalAccountAuthorizedUserFile
  55. if err := json.Unmarshal(b, &f); err != nil {
  56. return nil, err
  57. }
  58. return f, nil
  59. }
  60. // ParseImpersonatedServiceAccount parses bytes into a
  61. // [ImpersonatedServiceAccountFile].
  62. func ParseImpersonatedServiceAccount(b []byte) (*ImpersonatedServiceAccountFile, error) {
  63. var f *ImpersonatedServiceAccountFile
  64. if err := json.Unmarshal(b, &f); err != nil {
  65. return nil, err
  66. }
  67. return f, nil
  68. }
  69. // ParseGDCHServiceAccount parses bytes into a [GDCHServiceAccountFile].
  70. func ParseGDCHServiceAccount(b []byte) (*GDCHServiceAccountFile, error) {
  71. var f *GDCHServiceAccountFile
  72. if err := json.Unmarshal(b, &f); err != nil {
  73. return nil, err
  74. }
  75. return f, nil
  76. }
  77. type fileTypeChecker struct {
  78. Type string `json:"type"`
  79. }
  80. // ParseFileType determines the [CredentialType] based on bytes provided.
  81. func ParseFileType(b []byte) (CredentialType, error) {
  82. var f fileTypeChecker
  83. if err := json.Unmarshal(b, &f); err != nil {
  84. return 0, err
  85. }
  86. return parseCredentialType(f.Type), nil
  87. }