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