Repository navigation
feat: offline licenses authenticate the CLI without the platform #2127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ThomasRooney
wants to merge
2
commits into
main
Choose a base branch
from
feat/offline-license-auth
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,016
−31
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| package auth | ||
|
|
||
| import ( | ||
| "context" | ||
| "slices" | ||
| "testing" | ||
|
|
||
| core "github.com/speakeasy-api/speakeasy-core/auth" | ||
| "github.com/speakeasy-api/speakeasy/internal/license" | ||
| ) | ||
|
|
||
| func TestAuthenticateForceIgnoresExistingAPIKey(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| ctx := context.WithValue(context.Background(), licenseContextKey{}, &license.License{}) | ||
| ctx = context.WithValue(ctx, core.LicenseTokenKey, []byte("stale-license")) | ||
| freshLicense := []byte("fresh-license") | ||
| coreForce := true | ||
| persisted := false | ||
|
|
||
| authCtx, err := authenticate( | ||
| ctx, | ||
| "api-key", | ||
| true, | ||
| func(ctx context.Context, apiKey string, force bool) (context.Context, core.SpeakeasyAuthInfo, error) { | ||
| if apiKey != "api-key" { | ||
| t.Fatalf("API key = %q, want api-key", apiKey) | ||
| } | ||
| if licenseFromContext(ctx) != nil { | ||
| t.Fatal("offline license reached core authentication") | ||
| } | ||
| if token, ok := core.GetLicenseTokenFromContext(ctx); ok || len(token) != 0 { | ||
| t.Fatalf("stale license reached core authentication: %q", token) | ||
| } | ||
| coreForce = force | ||
| return context.WithValue(ctx, core.LicenseTokenKey, freshLicense), core.SpeakeasyAuthInfo{ | ||
| APIKey: apiKey, | ||
| WorkspaceID: "workspace", | ||
| }, nil | ||
| }, | ||
| func(ctx context.Context, info core.SpeakeasyAuthInfo) error { | ||
| persisted = true | ||
| if info.APIKey != "api-key" || info.WorkspaceID != "workspace" { | ||
| t.Fatalf("persisted auth info = %#v", info) | ||
| } | ||
| token, ok := core.GetLicenseTokenFromContext(ctx) | ||
| if !ok || !slices.Equal(token, freshLicense) { | ||
| t.Fatalf("persisted license = %q, want %q", token, freshLicense) | ||
| } | ||
| return nil | ||
| }, | ||
| ) | ||
| if err != nil { | ||
| t.Fatalf("authenticate: %v", err) | ||
| } | ||
| if !coreForce { | ||
| t.Fatal("force did not ignore the existing API key for browser authentication") | ||
| } | ||
| if !persisted { | ||
| t.Fatal("refreshed authentication was not persisted") | ||
| } | ||
| if token, ok := core.GetLicenseTokenFromContext(authCtx); !ok || !slices.Equal(token, freshLicense) { | ||
| t.Fatalf("authentication context license = %q, want %q", token, freshLicense) | ||
| } | ||
| } | ||
|
|
||
| func TestAuthenticateForceUsesBrowserWithoutAPIKey(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| coreForce := false | ||
| _, err := authenticate( | ||
| context.Background(), | ||
| "", | ||
| true, | ||
| func(ctx context.Context, _ string, force bool) (context.Context, core.SpeakeasyAuthInfo, error) { | ||
| coreForce = force | ||
| return ctx, core.SpeakeasyAuthInfo{}, nil | ||
| }, | ||
| func(context.Context, core.SpeakeasyAuthInfo) error { return nil }, | ||
| ) | ||
| if err != nil { | ||
| t.Fatalf("authenticate: %v", err) | ||
| } | ||
| if !coreForce { | ||
| t.Fatal("force did not request browser authentication without an API key") | ||
| } | ||
| } | ||
|
|
||
| func TestCommandContextFallsBackToPlatformWithoutOfflineLicense(t *testing.T) { | ||
| t.Setenv("SPEAKEASY_LICENSE_TOKEN", "not-a-license") | ||
|
ThomasRooney marked this conversation as resolved.
|
||
|
|
||
| wantCtx := context.WithValue(context.Background(), core.WorkspaceIDKey, "online-workspace") | ||
| called := false | ||
| ctx, err := commandContext(context.Background(), func(_ context.Context, force bool) (context.Context, error) { | ||
| called = true | ||
| if force { | ||
| t.Fatal("command context forced online re-authentication") | ||
| } | ||
| return wantCtx, nil | ||
| }) | ||
| if err != nil { | ||
| t.Fatalf("command context: %v", err) | ||
| } | ||
| if !called || ctx != wantCtx { | ||
| t.Fatal("command context did not fall back to platform authentication") | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.