@@ -1138,7 +1138,8 @@ func TestGrants_Authentication(t *testing.T) {
1138
1138
iamRepo := iam .TestRepo (t , conn , wrap )
1139
1139
atRepo , err := authtoken .NewRepository (ctx , rw , rw , kmsCache )
1140
1140
require .NoError (t , err )
1141
-
1141
+ pwRepo , err := password .NewRepository (ctx , rw , rw , kmsCache )
1142
+ require .NoError (t , err )
1142
1143
iamRepoFn := func () (* iam.Repository , error ) {
1143
1144
return iamRepo , nil
1144
1145
}
@@ -1149,7 +1150,7 @@ func TestGrants_Authentication(t *testing.T) {
1149
1150
return ldap .NewRepository (ctx , rw , rw , kmsCache )
1150
1151
}
1151
1152
pwRepoFn := func () (* password.Repository , error ) {
1152
- return password . NewRepository ( ctx , rw , rw , kmsCache )
1153
+ return pwRepo , nil
1153
1154
}
1154
1155
atRepoFn := func () (* authtoken.Repository , error ) {
1155
1156
return atRepo , nil
@@ -1170,9 +1171,6 @@ func TestGrants_Authentication(t *testing.T) {
1170
1171
require .NoError (t , err )
1171
1172
org1 , p1 := iam .TestScopes (t , iamRepo )
1172
1173
1173
- pwRepo , err := pwRepoFn ()
1174
- require .NoError (t , err )
1175
-
1176
1174
// We need a sys eventer in order to authenticate
1177
1175
eventConfig := event .TestEventerConfig (t , "TestGrants_WriteActions" , event .TestWithObservationSink (t ))
1178
1176
testLock := & sync.Mutex {}
@@ -1189,18 +1187,17 @@ func TestGrants_Authentication(t *testing.T) {
1189
1187
})
1190
1188
1191
1189
testcases := []struct {
1192
- name string
1193
- scopeId string
1194
- input * pbs.AuthenticateRequest
1195
- userFunc func () (* iam.User , auth.Account )
1196
- rolesToCreate []authtoken.TestRoleGrantsForToken
1197
- wantErr error
1190
+ name string
1191
+ scopeId string
1192
+ input * pbs.AuthenticateRequest
1193
+ userFunc func () (* iam.User , auth.Account )
1194
+ wantErr error
1198
1195
}{
1199
1196
{
1200
1197
name : "global role grant this and children can authenticate against a global auth method" ,
1201
1198
scopeId : globals .GlobalPrefix ,
1202
1199
input : & pbs.AuthenticateRequest {
1203
- TokenType : "token" ,
1200
+ Type : "token" ,
1204
1201
Attrs : & pbs.AuthenticateRequest_PasswordLoginAttributes {
1205
1202
PasswordLoginAttributes : & pbs.PasswordLoginAttributes {
1206
1203
LoginName : testLoginName ,
@@ -1215,50 +1212,49 @@ func TestGrants_Authentication(t *testing.T) {
1215
1212
GrantScopes : []string {globals .GrantScopeThis , globals .GrantScopeChildren },
1216
1213
},
1217
1214
}),
1218
- rolesToCreate : []authtoken.TestRoleGrantsForToken {{
1219
- RoleScopeId : globals .GlobalPrefix ,
1220
- GrantStrings : []string {"ids=*;type=auth-method;actions=authenticate" },
1221
- GrantScopes : []string {globals .GrantScopeThis , globals .GrantScopeChildren },
1222
- }},
1223
1215
},
1224
1216
{
1225
1217
name : "org role can't authenticate against a global auth method" ,
1226
1218
scopeId : globals .GlobalPrefix ,
1227
1219
input : & pbs.AuthenticateRequest {
1228
- TokenType : "token" ,
1220
+ Type : "token" ,
1229
1221
Attrs : & pbs.AuthenticateRequest_PasswordLoginAttributes {
1230
1222
PasswordLoginAttributes : & pbs.PasswordLoginAttributes {
1231
1223
LoginName : testLoginName ,
1232
1224
Password : testPassword ,
1233
1225
},
1234
1226
},
1235
1227
},
1236
- rolesToCreate : []authtoken.TestRoleGrantsForToken {{
1237
- RoleScopeId : org1 .PublicId ,
1238
- GrantStrings : []string {"ids=*;type=auth-method;actions=authenticate" },
1239
- GrantScopes : []string {globals .GrantScopeThis },
1240
- }},
1228
+ userFunc : iam .TestUserDirectGrantsFunc (t , conn , kmsCache , globals .GlobalPrefix , password .TestAuthMethodWithAccount , []iam.TestRoleGrantsRequest {
1229
+ {
1230
+ RoleScopeId : org1 .PublicId ,
1231
+ Grants : []string {"ids=*;type=auth-method;actions=authenticate" },
1232
+ GrantScopes : []string {globals .GrantScopeThis },
1233
+ },
1234
+ }),
1241
1235
wantErr : handlers .ForbiddenError (),
1242
1236
},
1243
1237
{
1244
1238
name : "no grants returns 403 error for a global auth method" ,
1245
1239
scopeId : globals .GlobalPrefix ,
1246
1240
input : & pbs.AuthenticateRequest {
1247
- TokenType : "token" ,
1241
+ Type : "token" ,
1248
1242
Attrs : & pbs.AuthenticateRequest_PasswordLoginAttributes {
1249
1243
PasswordLoginAttributes : & pbs.PasswordLoginAttributes {
1250
1244
LoginName : testLoginName ,
1251
1245
Password : testPassword ,
1252
1246
},
1253
1247
},
1254
1248
},
1255
- wantErr : handlers .ForbiddenError (),
1249
+ userFunc : iam .TestUserDirectGrantsFunc (t , conn , kmsCache , globals .GlobalPrefix , password .TestAuthMethodWithAccount , []iam.TestRoleGrantsRequest {}),
1250
+ wantErr : handlers .ForbiddenError (),
1256
1251
},
1257
1252
{
1258
- name : "org auth methods, by default, grant anyone permission to authenticate (and list) auth methods" ,
1259
- scopeId : org1 .PublicId ,
1253
+ name : "org auth methods, by default, grant anyone permission to authenticate (and list) auth methods" ,
1254
+ scopeId : org1 .PublicId ,
1255
+ userFunc : iam .TestUserDirectGrantsFunc (t , conn , kmsCache , globals .GlobalPrefix , password .TestAuthMethodWithAccount , []iam.TestRoleGrantsRequest {}),
1260
1256
input : & pbs.AuthenticateRequest {
1261
- TokenType : "token" ,
1257
+ Type : "token" ,
1262
1258
Attrs : & pbs.AuthenticateRequest_PasswordLoginAttributes {
1263
1259
PasswordLoginAttributes : & pbs.PasswordLoginAttributes {
1264
1260
LoginName : testLoginName ,
@@ -1271,90 +1267,83 @@ func TestGrants_Authentication(t *testing.T) {
1271
1267
name : "project role can authenticate against org auth methods because by default, org auth methods grant anyone permission to authenticate (and list) auth methods" ,
1272
1268
scopeId : org1 .PublicId ,
1273
1269
input : & pbs.AuthenticateRequest {
1274
- TokenType : "token" ,
1270
+ Type : "token" ,
1275
1271
Attrs : & pbs.AuthenticateRequest_PasswordLoginAttributes {
1276
1272
PasswordLoginAttributes : & pbs.PasswordLoginAttributes {
1277
1273
LoginName : testLoginName ,
1278
1274
Password : testPassword ,
1279
1275
},
1280
1276
},
1281
1277
},
1282
- rolesToCreate : []authtoken.TestRoleGrantsForToken {{
1283
- RoleScopeId : p1 .PublicId ,
1284
- GrantStrings : []string {"ids=*;type=auth-method;actions=authenticate" },
1285
- GrantScopes : []string {globals .GrantScopeThis },
1286
- }},
1278
+ userFunc : iam .TestUserDirectGrantsFunc (t , conn , kmsCache , globals .GlobalPrefix , password .TestAuthMethodWithAccount , []iam.TestRoleGrantsRequest {
1279
+ {
1280
+ RoleScopeId : p1 .PublicId ,
1281
+ Grants : []string {"ids=*;type=auth-method;actions=authenticate" },
1282
+ GrantScopes : []string {globals .GrantScopeThis },
1283
+ },
1284
+ }),
1287
1285
},
1288
1286
{
1289
1287
name : "granting authenticate again at the org scope allows authentication" ,
1290
1288
scopeId : org1 .PublicId ,
1291
1289
input : & pbs.AuthenticateRequest {
1292
- TokenType : "token" ,
1290
+ Type : "token" ,
1293
1291
Attrs : & pbs.AuthenticateRequest_PasswordLoginAttributes {
1294
1292
PasswordLoginAttributes : & pbs.PasswordLoginAttributes {
1295
1293
LoginName : testLoginName ,
1296
1294
Password : testPassword ,
1297
1295
},
1298
1296
},
1299
1297
},
1300
- rolesToCreate : []authtoken.TestRoleGrantsForToken {{
1301
- RoleScopeId : org1 .PublicId ,
1302
- GrantStrings : []string {"ids=*;type=auth-method;actions=authenticate" },
1303
- GrantScopes : []string {globals .GrantScopeThis },
1304
- }},
1298
+ userFunc : iam .TestUserDirectGrantsFunc (t , conn , kmsCache , globals .GlobalPrefix , password .TestAuthMethodWithAccount , []iam.TestRoleGrantsRequest {
1299
+ {
1300
+ RoleScopeId : org1 .PublicId ,
1301
+ Grants : []string {"ids=*;type=auth-method;actions=authenticate" },
1302
+ GrantScopes : []string {globals .GrantScopeThis },
1303
+ },
1304
+ }),
1305
1305
},
1306
1306
{
1307
1307
name : "project role can't authenticate against global auth methods" ,
1308
1308
scopeId : globals .GlobalPrefix ,
1309
1309
input : & pbs.AuthenticateRequest {
1310
- TokenType : "token" ,
1310
+ Type : "token" ,
1311
1311
Attrs : & pbs.AuthenticateRequest_PasswordLoginAttributes {
1312
1312
PasswordLoginAttributes : & pbs.PasswordLoginAttributes {
1313
1313
LoginName : testLoginName ,
1314
1314
Password : testPassword ,
1315
1315
},
1316
1316
},
1317
1317
},
1318
- rolesToCreate : []authtoken.TestRoleGrantsForToken {{
1319
- RoleScopeId : p1 .PublicId ,
1320
- GrantStrings : []string {"ids=*;type=auth-method;actions=authenticate" },
1321
- GrantScopes : []string {globals .GrantScopeThis },
1322
- }},
1318
+ userFunc : iam .TestUserDirectGrantsFunc (t , conn , kmsCache , globals .GlobalPrefix , password .TestAuthMethodWithAccount , []iam.TestRoleGrantsRequest {
1319
+ {
1320
+ RoleScopeId : p1 .PublicId ,
1321
+ Grants : []string {"ids=*;type=auth-method;actions=authenticate" },
1322
+ GrantScopes : []string {globals .GrantScopeThis },
1323
+ },
1324
+ }),
1323
1325
wantErr : handlers .ForbiddenError (),
1324
1326
},
1325
1327
}
1326
1328
1327
1329
for _ , tc := range testcases {
1328
1330
t .Run (tc .name , func (t * testing.T ) {
1329
- // Create auth method
1330
- if tc .scopeId == globals .GlobalPrefix {
1331
- tc .input .AuthMethodId = password .TestAuthMethod (t , conn , globals .GlobalPrefix ).PublicId
1332
- } else {
1333
- tc .input .AuthMethodId = password .TestAuthMethod (t , conn , org1 .PublicId ).PublicId
1334
- }
1335
-
1336
- // Create account for the auth method
1337
- account , err := password .NewAccount (ctx , tc .input .AuthMethodId , password .WithLoginName (testLoginName ))
1331
+ // set up an identity that we'll use to call authenticate request on. We'll use this credentials
1332
+ // and auth method to make authenticate request on
1333
+ tc .input .AuthMethodId = password .TestAuthMethod (t , conn , tc .scopeId ).PublicId
1334
+ newAcct , err := password .NewAccount (ctx , tc .input .AuthMethodId , password .WithLoginName (testLoginName ))
1338
1335
require .NoError (t , err )
1339
- account , err = pwRepo .CreateAccount (context .Background (), tc .scopeId , account , password .WithPassword (testPassword ))
1336
+ acctToLogin , err : = pwRepo .CreateAccount (context .Background (), tc .scopeId , newAcct , password .WithPassword (testPassword ))
1340
1337
require .NoError (t , err )
1341
- require . NotNil (t , account )
1338
+ user := iam . TestUser (t , iamRepo , tc . scopeId , iam . WithAccountIds ( acctToLogin . PublicId ) )
1342
1339
1343
- // Create user linked to the account
1344
- user := iam .TestUser (t , iamRepo , tc .scopeId , iam .WithAccountIds (account .PublicId ))
1345
-
1346
- // Create the desired role/grants for the user
1347
- for _ , roleToCreate := range tc .rolesToCreate {
1348
- role := iam .TestRoleWithGrants (t , conn , roleToCreate .RoleScopeId , roleToCreate .GrantScopes , roleToCreate .GrantStrings )
1349
- iam .TestUserRole (t , conn , role .PublicId , user .PublicId )
1350
- }
1351
-
1352
- // Create auth token for the user
1353
- tok , err := atRepo .CreateAuthToken (ctx , user , account .PublicId )
1340
+ // set up an identity that we'll use to call authenticate request
1341
+ // Authentication API will rely on this user's grants to authorize the request
1342
+ user , acctLoggingIn := tc .userFunc ()
1343
+ tok , err := atRepo .CreateAuthToken (ctx , user , acctLoggingIn .GetPublicId ())
1354
1344
require .NoError (t , err )
1355
- ctx := controllerauth .TestAuthContextFromToken (t , conn , wrap , tok , iamRepo )
1356
-
1357
- _ , err = s .Authenticate (ctx , tc .input )
1345
+ fullGrantAuthCtx := controllerauth .TestAuthContextFromToken (t , conn , wrap , tok , iamRepo )
1346
+ _ , err = s .Authenticate (fullGrantAuthCtx , tc .input )
1358
1347
if tc .wantErr != nil {
1359
1348
require .ErrorIs (t , err , tc .wantErr )
1360
1349
return
0 commit comments