MCPcopy Create free account
hub / github.com/bytebase/bytebase / authTest

Method authTest

backend/plugin/webhook/slack/app.go:60–106  ·  view source on GitHub ↗

https://api.slack.com/methods/auth.test

(ctx context.Context)

Source from the content-addressed store, hash-verified

58
59// https://api.slack.com/methods/auth.test
60func (p *provider) authTest(ctx context.Context) error {
61 req, err := http.NewRequestWithContext(ctx, http.MethodPost, "https://slack.com/api/auth.test", nil)
62 if err != nil {
63 return errors.Wrapf(err, "failed to new request")
64 }
65 req.Header.Add("Authorization", "Bearer "+p.token)
66 req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
67
68 resp, err := p.c.Do(req)
69 if err != nil {
70 return errors.Wrapf(err, "failed to send request")
71 }
72 defer resp.Body.Close()
73
74 if resp.StatusCode != http.StatusOK {
75 return errors.Errorf("received non-200 status code %d", resp.StatusCode)
76 }
77
78 body, err := io.ReadAll(resp.Body)
79 if err != nil {
80 return errors.Wrapf(err, "failed to read body")
81 }
82 var res authTestResponse
83 if err := json.Unmarshal(body, &res); err != nil {
84 return errors.Wrapf(err, "failed to unmarshal")
85 }
86 if !res.OK {
87 return errors.Errorf("failed to test auth, error: %v", res.Error)
88 }
89
90 scopes := resp.Header.Get("x-oauth-scopes")
91 hasScope := map[string]bool{}
92 for _, s := range strings.Split(scopes, ",") {
93 hasScope[s] = true
94 }
95 var missScope []string
96 for _, s := range []string{"users:read", "users:read.email", "channels:manage", "groups:write", "im:write", "chat:write", "mpim:write"} {
97 if !hasScope[s] {
98 missScope = append(missScope, s)
99 }
100 }
101 if len(missScope) > 0 {
102 return errors.Errorf("missing the following scopes: %s", strings.Join(missScope, ","))
103 }
104
105 return nil
106}
107
108var userIDCache = func() *lru.Cache[string, string] {
109 cache, err := lru.New[string, string](5000)

Callers 1

ValidateTokenFunction · 0.80

Calls 7

DoMethod · 0.80
ErrorfMethod · 0.80
UnmarshalMethod · 0.80
GetMethod · 0.80
JoinMethod · 0.80
CloseMethod · 0.65
SetMethod · 0.45

Tested by

no test coverage detected