Skip to content

Commit e4bf66a

Browse files
authored
githubauth: add oauth2 token sources (#344)
1 parent 042836e commit e4bf66a

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

githubauth/app_installation.go

+42
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ import (
2121
"fmt"
2222
"io"
2323
"net/http"
24+
"time"
25+
26+
"golang.org/x/oauth2"
2427
)
2528

2629
// AppInstallation represents a specific installation of the app (on a repo,
@@ -67,6 +70,26 @@ func (i *AppInstallation) SelectedReposTokenSource(permissions map[string]string
6770
})
6871
}
6972

73+
// SelectedReposOAuth2TokenSource creates an [oauth2.TokenSource] which can be
74+
// used in combination with [oauth2.NewClient] to create an authenticated HTTP
75+
// client capable of being passed to the go-github library.
76+
func (i *AppInstallation) SelectedReposOAuth2TokenSource(ctx context.Context, permissions map[string]string, repos ...string) oauth2.TokenSource {
77+
return oauth2.ReuseTokenSource(nil, oauth2TokenSource(func() (*oauth2.Token, error) {
78+
token, err := i.AccessToken(ctx, &TokenRequest{
79+
Permissions: permissions,
80+
Repositories: repos,
81+
})
82+
if err != nil {
83+
return nil, fmt.Errorf("failed to get github access token for repos %q: %w", repos, err)
84+
}
85+
86+
return &oauth2.Token{
87+
AccessToken: token,
88+
Expiry: time.Now().Add(55 * time.Minute), // GitHub's expiration is 1 hour
89+
}, nil
90+
}))
91+
}
92+
7093
// AccessTokenAllRepos calls the GitHub API to generate a new access token for
7194
// this application installation with the requested permissions and all granted
7295
// repositories.
@@ -93,6 +116,25 @@ func (i *AppInstallation) AllReposTokenSource(permissions map[string]string) Tok
93116
})
94117
}
95118

119+
// AllReposOAuth2TokenSource creates an [oauth2.TokenSource] which can be used
120+
// in combination with [oauth2.NewClient] to create an authenticated HTTP client
121+
// capable of being passed to the go-github library.
122+
func (i *AppInstallation) AllReposOAuth2TokenSource(ctx context.Context, permissions map[string]string) oauth2.TokenSource {
123+
return oauth2.ReuseTokenSource(nil, oauth2TokenSource(func() (*oauth2.Token, error) {
124+
token, err := i.AccessTokenAllRepos(ctx, &TokenRequestAllRepos{
125+
Permissions: permissions,
126+
})
127+
if err != nil {
128+
return nil, fmt.Errorf("failed to get github access token for all repos: %w", err)
129+
}
130+
131+
return &oauth2.Token{
132+
AccessToken: token,
133+
Expiry: time.Now().Add(55 * time.Minute), // GitHub's expiration is 1 hour
134+
}, nil
135+
}))
136+
}
137+
96138
// githubAccessToken calls the GitHub API to generate a new access token with
97139
// provided JSON payload bytes.
98140
func (i *AppInstallation) githubAccessToken(ctx context.Context, requestJSON []byte) (string, error) {

0 commit comments

Comments
 (0)