-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds new login method handling to the model proxy.
- Loading branch information
1 parent
12c20db
commit 4627413
Showing
12 changed files
with
768 additions
and
214 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
|
@@ -195,17 +195,15 @@ func TestJWTGeneratorMakeLoginToken(t *testing.T) { | |
|
||
tests := []struct { | ||
about string | ||
authenticator *testAuthenticator | ||
username string | ||
database *testDatabase | ||
accessChecker *testAccessChecker | ||
jwtService *testJWTService | ||
expectedError string | ||
expectedJWTParams jimmjwx.JWTParams | ||
}{{ | ||
about: "initial login, all is well", | ||
authenticator: &testAuthenticator{ | ||
username: "[email protected]", | ||
}, | ||
about: "initial login, all is well", | ||
username: "[email protected]", | ||
database: &testDatabase{ | ||
ctl: dbmodel.Controller{ | ||
CloudRegions: []dbmodel.CloudRegionControllerPriority{{ | ||
|
@@ -239,27 +237,16 @@ func TestJWTGeneratorMakeLoginToken(t *testing.T) { | |
}, | ||
}, | ||
}, { | ||
about: "authorization fails", | ||
authenticator: &testAuthenticator{ | ||
username: "[email protected]", | ||
err: errors.E("a test error"), | ||
}, | ||
expectedError: "a test error", | ||
}, { | ||
about: "model access check fails", | ||
authenticator: &testAuthenticator{ | ||
username: "[email protected]", | ||
}, | ||
about: "model access check fails", | ||
username: "[email protected]", | ||
accessChecker: &testAccessChecker{ | ||
modelAccessCheckErr: errors.E("a test error"), | ||
}, | ||
jwtService: &testJWTService{}, | ||
expectedError: "a test error", | ||
}, { | ||
about: "controller access check fails", | ||
authenticator: &testAuthenticator{ | ||
username: "[email protected]", | ||
}, | ||
about: "controller access check fails", | ||
username: "[email protected]", | ||
accessChecker: &testAccessChecker{ | ||
modelAccess: map[string]string{ | ||
mt.String(): "admin", | ||
|
@@ -268,10 +255,8 @@ func TestJWTGeneratorMakeLoginToken(t *testing.T) { | |
}, | ||
expectedError: "a test error", | ||
}, { | ||
about: "get controller from db fails", | ||
authenticator: &testAuthenticator{ | ||
username: "[email protected]", | ||
}, | ||
about: "get controller from db fails", | ||
username: "[email protected]", | ||
database: &testDatabase{ | ||
err: errors.E("a test error"), | ||
}, | ||
|
@@ -285,10 +270,8 @@ func TestJWTGeneratorMakeLoginToken(t *testing.T) { | |
}, | ||
expectedError: "failed to fetch controller", | ||
}, { | ||
about: "cloud access check fails", | ||
authenticator: &testAuthenticator{ | ||
username: "[email protected]", | ||
}, | ||
about: "cloud access check fails", | ||
username: "[email protected]", | ||
database: &testDatabase{ | ||
ctl: dbmodel.Controller{ | ||
CloudRegions: []dbmodel.CloudRegionControllerPriority{{ | ||
|
@@ -311,10 +294,8 @@ func TestJWTGeneratorMakeLoginToken(t *testing.T) { | |
}, | ||
expectedError: "failed to check user's cloud access", | ||
}, { | ||
about: "jwt service errors out", | ||
authenticator: &testAuthenticator{ | ||
username: "[email protected]", | ||
}, | ||
about: "jwt service errors out", | ||
username: "[email protected]", | ||
database: &testDatabase{ | ||
ctl: dbmodel.Controller{ | ||
CloudRegions: []dbmodel.CloudRegionControllerPriority{{ | ||
|
@@ -344,10 +325,14 @@ func TestJWTGeneratorMakeLoginToken(t *testing.T) { | |
}} | ||
|
||
for _, test := range tests { | ||
generator := jimm.NewJWTGenerator(test.authenticator, test.database, test.accessChecker, test.jwtService) | ||
generator := jimm.NewJWTGenerator(test.database, test.accessChecker, test.jwtService) | ||
generator.SetTags(mt, ct) | ||
|
||
_, err := generator.MakeLoginToken(context.Background(), &jujuparams.LoginRequest{}) | ||
_, err := generator.MakeLoginToken(context.Background(), &openfga.User{ | ||
Identity: &dbmodel.Identity{ | ||
Name: test.username, | ||
}, | ||
}) | ||
if test.expectedError != "" { | ||
c.Assert(err, qt.ErrorMatches, test.expectedError) | ||
} else { | ||
|
@@ -414,9 +399,6 @@ func TestJWTGeneratorMakeToken(t *testing.T) { | |
|
||
for _, test := range tests { | ||
generator := jimm.NewJWTGenerator( | ||
&testAuthenticator{ | ||
username: "[email protected]", | ||
}, | ||
&testDatabase{ | ||
ctl: dbmodel.Controller{ | ||
CloudRegions: []dbmodel.CloudRegionControllerPriority{{ | ||
|
@@ -445,7 +427,11 @@ func TestJWTGeneratorMakeToken(t *testing.T) { | |
) | ||
generator.SetTags(mt, ct) | ||
|
||
_, err := generator.MakeLoginToken(context.Background(), &jujuparams.LoginRequest{}) | ||
_, err := generator.MakeLoginToken(context.Background(), &openfga.User{ | ||
Identity: &dbmodel.Identity{ | ||
Name: "[email protected]", | ||
}, | ||
}) | ||
c.Assert(err, qt.IsNil) | ||
|
||
_, err = generator.MakeToken(context.Background(), test.permissions) | ||
|
This file contains 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,55 @@ | ||
// Copyright 2024 Canonical Ltd. | ||
|
||
package jimm | ||
|
||
import ( | ||
"context" | ||
|
||
"golang.org/x/oauth2" | ||
|
||
"github.com/canonical/jimm/internal/errors" | ||
) | ||
|
||
// LoginDevice starts the device login flow. | ||
func LoginDevice(ctx context.Context, authenticator OAuthAuthenticator) (*oauth2.DeviceAuthResponse, error) { | ||
const op = errors.Op("jujuapi.LoginDevice") | ||
|
||
deviceResponse, err := authenticator.Device(ctx) | ||
if err != nil { | ||
return nil, errors.E(op, err) | ||
} | ||
|
||
return deviceResponse, nil | ||
} | ||
|
||
func GetDeviceSessionToken(ctx context.Context, authenticator OAuthAuthenticator, deviceOAuthResponse *oauth2.DeviceAuthResponse) (string, error) { | ||
const op = errors.Op("jujuapi.GetDeviceSessionToken") | ||
|
||
token, err := authenticator.DeviceAccessToken(ctx, deviceOAuthResponse) | ||
if err != nil { | ||
return "", errors.E(op, err) | ||
} | ||
|
||
idToken, err := authenticator.ExtractAndVerifyIDToken(ctx, token) | ||
if err != nil { | ||
return "", errors.E(op, err) | ||
} | ||
|
||
email, err := authenticator.Email(idToken) | ||
if err != nil { | ||
return "", errors.E(op, err) | ||
} | ||
|
||
if err := authenticator.UpdateIdentity(ctx, email, token); err != nil { | ||
return "", errors.E(op, err) | ||
} | ||
|
||
// TODO(ale8k): Add vault logic to get secret key and generate one | ||
// on start up. | ||
encToken, err := authenticator.MintSessionToken(email, "test-secret") | ||
if err != nil { | ||
return "", errors.E(op, err) | ||
} | ||
|
||
return string(encToken), nil | ||
} |
This file contains 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 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 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 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 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.