Skip to content
This repository was archived by the owner on May 24, 2024. It is now read-only.

Commit 1806c8b

Browse files
authored
Merge pull request #18 from LF-Engineering/add-common-util
Add common util
2 parents 4b737a2 + e3fe24c commit 1806c8b

File tree

4 files changed

+533
-10
lines changed

4 files changed

+533
-10
lines changed

affiliation/dto.go

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,6 @@ type EnrollmentsResponse struct {
4848
UUID string `json:"uuid"`
4949
}
5050

51-
// Profile ...
52-
type Profile struct {
53-
IsBot int `json:"is_bot"`
54-
Name string `json:"name"`
55-
UUID string `json:"uuid"`
56-
Email string `json:"email"`
57-
Gender string `json:"gender"`
58-
GenderAcc int `json:"gender_acc"`
59-
}
60-
6151
// ProfileResponse ...
6252
type ProfileResponse struct {
6353
Enrollments []Enrollment `json:"enrollments"`
@@ -66,3 +56,57 @@ type ProfileResponse struct {
6656
Profile Profile `json:"profile"`
6757
UUID string `json:"uuid"`
6858
}
59+
60+
// IdentityData ...
61+
type IdentityData struct {
62+
Email *string `json:"email,omitempty"`
63+
ID string `json:"id,omitempty"`
64+
LastModified *time.Time `json:"last_modified,omitempty"`
65+
Name *string `json:"name,omitempty"`
66+
Source string `json:"source,omitempty"`
67+
Username *string `json:"username,omitempty"`
68+
UUID *string `json:"uuid,omitempty"`
69+
}
70+
71+
// UniqueIdentityFullProfile ...
72+
type UniqueIdentityFullProfile struct {
73+
Enrollments []*Enrollments `json:"enrollments"`
74+
Identities []*IdentityData `json:"identities"`
75+
Profile *Profile `json:"profile,omitempty"`
76+
UUID string `json:"uuid,omitempty"`
77+
}
78+
79+
// Enrollments ...
80+
type Enrollments struct {
81+
Organization *Organization `json:"organization,omitempty"`
82+
}
83+
84+
// Organization ...
85+
type Organization struct {
86+
Name string `json:"name,omitempty"`
87+
}
88+
89+
// Profile ...
90+
type Profile struct {
91+
Email *string `json:"email,omitempty"`
92+
Gender *string `json:"gender,omitempty"`
93+
GenderAcc *int64 `json:"gender_acc,omitempty"`
94+
IsBot *int64 `json:"is_bot,omitempty"`
95+
Name *string `json:"name,omitempty"`
96+
UUID string `json:"uuid,omitempty"`
97+
}
98+
99+
// AffIdentity contains affiliation user Identity
100+
type AffIdentity struct {
101+
ID *string `json:"id"`
102+
UUID *string
103+
Name string
104+
Username string
105+
Email string
106+
Domain string
107+
Gender *string `json:"gender"`
108+
GenderACC *int64 `json:"gender_acc"`
109+
OrgName *string `json:"org_name"`
110+
IsBot *int64 `json:"is_bot"`
111+
MultiOrgNames []string `json:"multi_org_names"`
112+
}

affiliation/identity.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,71 @@ func (a *Affiliation) GetProfile(uuid, projectSlug string) *ProfileResponse {
190190
return &response
191191
}
192192

193+
// GetIdentityByUser ...
194+
func (a *Affiliation) GetIdentityByUser(key string, value string) (*AffIdentity, error) {
195+
if key == "" || value == "" {
196+
nilKeyOrValueErr := "repository: GetIdentityByUser: key or value is null"
197+
log.Println(nilKeyOrValueErr)
198+
return nil, fmt.Errorf(nilKeyOrValueErr)
199+
}
200+
token, err := a.auth0Client.ValidateToken(a.Environment)
201+
if err != nil {
202+
log.Println(err)
203+
return nil, err
204+
}
205+
206+
headers := make(map[string]string, 0)
207+
headers["Authorization"] = fmt.Sprintf("%s %s", "Bearer", token)
208+
endpoint := a.AffBaseURL + "/affiliation/" + "identity/" + key + "/" + value
209+
_, res, err := a.httpClient.Request(strings.TrimSpace(endpoint), "GET", headers, nil, nil)
210+
if err != nil {
211+
log.Println("Repository: GetIdentityByUser: Could not get the identity: ", err)
212+
return nil, err
213+
}
214+
215+
var ident IdentityData
216+
err = json.Unmarshal(res, &ident)
217+
if err != nil {
218+
return nil, err
219+
}
220+
221+
profileEndpoint := a.AffBaseURL + "/affiliation/" + url.PathEscape(a.ProjectSlug) + "/get_profile/" + *ident.UUID
222+
_, profileRes, err := a.httpClient.Request(strings.TrimSpace(profileEndpoint), "GET", headers, nil, nil)
223+
if err != nil {
224+
log.Println("Repository: GetIdentityByUser: Could not get the identity: ", err)
225+
return nil, err
226+
}
227+
228+
var profile UniqueIdentityFullProfile
229+
err = json.Unmarshal(profileRes, &profile)
230+
if err != nil {
231+
return nil, err
232+
}
233+
var identity AffIdentity
234+
identity.UUID = ident.UUID
235+
identity.Name = *ident.Name
236+
identity.Username = *ident.Username
237+
identity.Email = *ident.Email
238+
identity.ID = &ident.ID
239+
240+
identity.IsBot = profile.Profile.IsBot
241+
identity.Gender = profile.Profile.Gender
242+
identity.GenderACC = profile.Profile.GenderAcc
243+
244+
if len(profile.Enrollments) > 1 {
245+
identity.OrgName = &profile.Enrollments[0].Organization.Name
246+
for _, org := range profile.Enrollments {
247+
identity.MultiOrgNames = append(identity.MultiOrgNames, org.Organization.Name)
248+
}
249+
} else if len(profile.Enrollments) == 1 {
250+
identity.OrgName = &profile.Enrollments[0].Organization.Name
251+
identity.MultiOrgNames = append(identity.MultiOrgNames, profile.Enrollments[0].Organization.Name)
252+
}
253+
254+
return &identity, nil
255+
256+
}
257+
193258
func buildServices(a *Affiliation) (httpClientProvider *http.ClientProvider, esClientProvider *elastic.ClientProvider, auth0ClientProvider *auth0.ClientProvider, err error) {
194259
esClientProvider, err = elastic.NewClientProvider(&elastic.Params{
195260
URL: a.ESCacheURL,

go.mod

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,16 @@ go 1.15
55
require (
66
github.com/avast/retry-go v3.0.0+incompatible
77
github.com/aws/aws-sdk-go v1.36.15
8+
github.com/dgrijalva/jwt-go v3.2.0+incompatible
89
github.com/elastic/go-elasticsearch/v7 v7.10.0
10+
github.com/go-git/go-git/v5 v5.2.0
11+
github.com/google/go-github v17.0.0+incompatible
12+
github.com/google/go-querystring v1.0.0 // indirect
13+
github.com/google/uuid v1.1.4
914
github.com/json-iterator/go v1.1.10
1015
github.com/stretchr/testify v1.6.1
1116
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad
17+
golang.org/x/oauth2 v0.0.0-20201208152858-08078c50e5b5
1218
golang.org/x/text v0.3.4
19+
gopkg.in/yaml.v2 v2.2.8
1320
)

0 commit comments

Comments
 (0)