MCPcopy
hub / github.com/pocketbase/pocketbase / fetchVerifiedPrimaryEmail

Method fetchVerifiedPrimaryEmail

tools/auth/gitea.go:101–138  ·  view source on GitHub ↗

fetchVerifiedPrimaryEmail sends an API request to retrieve the verified primary email, in case "Keep my email address private" was set. NB! This method can succeed and still return an empty email. Error responses that are result of insufficient scopes permissions are ignored. API reference: https:

(token *oauth2.Token)

Source from the content-addressed store, hash-verified

99//
100// API reference: https://codeberg.org/api/swagger#/user/userListEmails
101func (p *Gitea) fetchVerifiedPrimaryEmail(token *oauth2.Token) (string, error) {
102 client := p.Client(token)
103
104 response, err := client.Get(p.userInfoURL + "/emails")
105 if err != nil {
106 return "", err
107 }
108 defer response.Body.Close()
109
110 // ignore common http errors caused by insufficient scope permissions
111 // (the email field is optional, aka. return the auth user without it)
112 if response.StatusCode == 401 || response.StatusCode == 403 || response.StatusCode == 404 {
113 return "", nil
114 }
115
116 content, err := io.ReadAll(response.Body)
117 if err != nil {
118 return "", err
119 }
120
121 emails := []struct {
122 Email string
123 Verified bool
124 Primary bool
125 }{}
126 if err := json.Unmarshal(content, &emails); err != nil {
127 return "", err
128 }
129
130 // extract the verified primary email
131 for _, email := range emails {
132 if email.Verified && email.Primary {
133 return email.Email, nil
134 }
135 }
136
137 return "", nil
138}

Callers 1

FetchAuthUserMethod · 0.95

Calls 3

ClientMethod · 0.65
GetMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected