| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"os" |
| 7 |
|
| 8 |
"github.com/google/go-github/v31/github" |
| 9 |
"github.com/google/uuid" |
| 10 |
"golang.org/x/oauth2" |
| 11 |
githubOAuth "golang.org/x/oauth2/github" |
| 12 |
|
| 13 |
"github.com/zitadel/oidc/v3/pkg/client/rp" |
| 14 |
"github.com/zitadel/oidc/v3/pkg/client/rp/cli" |
| 15 |
"github.com/zitadel/oidc/v3/pkg/http" |
| 16 |
"github.com/zitadel/oidc/v3/pkg/oidc" |
| 17 |
) |
| 18 |
|
| 19 |
var ( |
| 20 |
callbackPath = "/orbctl/github/callback" |
| 21 |
key = []byte("test1234test1234") |
| 22 |
) |
| 23 |
|
| 24 |
func main() { |
| 25 |
clientID := os.Getenv("CLIENT_ID") |
| 26 |
clientSecret := os.Getenv("CLIENT_SECRET") |
| 27 |
port := os.Getenv("PORT") |
| 28 |
|
| 29 |
rpConfig := &oauth2.Config{ |
| 30 |
ClientID: clientID, |
| 31 |
ClientSecret: clientSecret, |
| 32 |
RedirectURL: fmt.Sprintf("http://127.0.0.1:%v%v", port, callbackPath), |
| 33 |
Scopes: []string{"repo", "repo_deployment"}, |
| 34 |
Endpoint: githubOAuth.Endpoint, |
| 35 |
} |
| 36 |
|
| 37 |
ctx := context.Background() |
| 38 |
cookieHandler := http.NewCookieHandler(key, key, http.WithUnsecure()) |
| 39 |
relyingParty, err := rp.NewRelyingPartyOAuth(rpConfig, rp.WithCookieHandler(cookieHandler)) |
| 40 |
if err != nil { |
| 41 |
fmt.Printf("error creating relaying party: %v", err) |
| 42 |
return |
| 43 |
} |
| 44 |
state := func() string { |
| 45 |
return uuid.New().String() |
| 46 |
} |
| 47 |
token := cli.CodeFlow[*oidc.IDTokenClaims](ctx, relyingParty, callbackPath, port, state) |
| 48 |
|
| 49 |
client := github.NewClient(relyingParty.OAuthConfig().Client(ctx, token.Token)) |
| 50 |
|
| 51 |
_, _, err = client.Users.Get(ctx, "") |
| 52 |
if err != nil { |
| 53 |
fmt.Printf("error %v", err) |
| 54 |
return |
| 55 |
} |
| 56 |
fmt.Println("call succeeded") |
| 57 |
} |