-
Notifications
You must be signed in to change notification settings - Fork 959
Expand file tree
/
Copy pathapps.go
More file actions
79 lines (64 loc) · 2.24 KB
/
apps.go
File metadata and controls
79 lines (64 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package github
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"time"
"github.com/go-jose/go-jose/v3/jwt"
)
// Signer is an interface for signing JWTs.
// It allow for different implementations (e.g., using a local PEM file or delegating to AWS KMS).
type Signer interface {
SignJWT(ctx context.Context, claims jwt.Claims) (string, error)
}
// GenerateOAuthTokenFromApp generates a GitHub OAuth access token from a set of valid GitHub App credentials.
// The returned token can be used to interact with both GitHub's REST and GraphQL APIs.
func GenerateOAuthTokenFromApp(ctx context.Context, signer Signer, apiURL *url.URL, appID, appInstallationID string) (string, error) {
appJWT, err := generateAppJWT(ctx, appID, time.Now(), signer)
if err != nil {
return "", err
}
return getInstallationAccessToken(apiURL, appJWT, appInstallationID)
}
func getInstallationAccessToken(apiURL *url.URL, jwt, installationID string) (string, error) {
req, err := http.NewRequest(http.MethodPost, apiURL.JoinPath("app/installations", installationID, "access_tokens").String(), nil)
if err != nil {
return "", err
}
req.Header.Add("Accept", "application/vnd.github.v3+json")
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", jwt))
res, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
defer func() { _ = res.Body.Close() }()
resBytes, err := io.ReadAll(res.Body)
if err != nil {
return "", err
}
if res.StatusCode != http.StatusCreated {
return "", fmt.Errorf("failed to create OAuth token from GitHub App: %s", string(resBytes))
}
resData := struct {
Token string `json:"token"`
}{}
err = json.Unmarshal(resBytes, &resData)
if err != nil {
return "", err
}
return resData.Token, nil
}
func generateAppJWT(ctx context.Context, appID string, now time.Time, signer Signer) (string, error) {
claims := jwt.Claims{
Issuer: appID,
// Using now - 60s to accommodate any client/server clock drift.
IssuedAt: jwt.NewNumericDate(now.Add(time.Duration(-60) * time.Second)),
// The JWT's lifetime can be short as it is only used immediately
// after to retrieve the installation's access token.
Expiry: jwt.NewNumericDate(now.Add(time.Duration(5) * time.Minute)),
}
return signer.SignJWT(ctx, claims)
}