Skip to content

Commit 48bc985

Browse files
authored
chore(auth): set organization in auth token flow (#858)
Signed-off-by: Jose I. Paris <[email protected]>
1 parent f81a43e commit 48bc985

File tree

3 files changed

+53
-45
lines changed

3 files changed

+53
-45
lines changed

app/controlplane/cmd/wire_gen.go

Lines changed: 33 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/controlplane/internal/server/grpc.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ import (
5050

5151
type Opts struct {
5252
// UseCases
53-
UserUseCase *biz.UserUseCase
54-
RobotAccountUseCase *biz.RobotAccountUseCase
55-
CASBackendUseCase *biz.CASBackendUseCase
56-
CASClientUseCase *biz.CASClientUseCase
57-
IntegrationUseCase *biz.IntegrationUseCase
58-
ReferrerUseCase *biz.ReferrerUseCase
59-
APITokenUseCase *biz.APITokenUseCase
60-
OrganizationUserCase *biz.OrganizationUseCase
61-
WorkflowUseCase *biz.WorkflowUseCase
53+
UserUseCase *biz.UserUseCase
54+
RobotAccountUseCase *biz.RobotAccountUseCase
55+
CASBackendUseCase *biz.CASBackendUseCase
56+
CASClientUseCase *biz.CASClientUseCase
57+
IntegrationUseCase *biz.IntegrationUseCase
58+
ReferrerUseCase *biz.ReferrerUseCase
59+
APITokenUseCase *biz.APITokenUseCase
60+
OrganizationUseCase *biz.OrganizationUseCase
61+
WorkflowUseCase *biz.WorkflowUseCase
6262
// Services
6363
WorkflowSvc *service.WorkflowService
6464
AuthSvc *service.AuthService
@@ -178,7 +178,7 @@ func craftMiddleware(opts *Opts) []middleware.Middleware {
178178
// 2.a - Set its user and organization
179179
usercontext.WithCurrentUserAndOrgMiddleware(opts.UserUseCase, logHelper),
180180
// 2.b - Set its API token and organization as alternative to the user
181-
usercontext.WithCurrentAPITokenAndOrgMiddleware(opts.APITokenUseCase, opts.OrganizationUserCase, logHelper),
181+
usercontext.WithCurrentAPITokenAndOrgMiddleware(opts.APITokenUseCase, opts.OrganizationUseCase, logHelper),
182182
// 3 - Check user/token authorization
183183
authzMiddleware.WithAuthzMiddleware(opts.Enforcer, logHelper),
184184
// 4 - Make sure the account is fully functional
@@ -201,7 +201,7 @@ func craftMiddleware(opts *Opts) []middleware.Middleware {
201201
attjwtmiddleware.NewAPITokenProvider(opts.AuthConfig.GeneratedJwsHmacSecret),
202202
),
203203
// 2.a - Set its workflow and organization in the context
204-
usercontext.WithAttestationContextFromRobotAccount(opts.RobotAccountUseCase, logHelper),
204+
usercontext.WithAttestationContextFromRobotAccount(opts.RobotAccountUseCase, opts.OrganizationUseCase, logHelper),
205205
// 2.b - Set its API token and Robot Account as alternative to the user
206206
usercontext.WithAttestationContextFromAPIToken(opts.APITokenUseCase, logHelper),
207207
).Match(requireRobotAccountMatcher()).Build(),

app/controlplane/internal/usercontext/robotaccount_middleware.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package usercontext
1818
import (
1919
"context"
2020
"errors"
21+
"fmt"
2122

2223
"github.com/chainloop-dev/chainloop/app/controlplane/internal/biz"
2324
"github.com/chainloop-dev/chainloop/app/controlplane/internal/jwt/robotaccount"
@@ -46,7 +47,7 @@ func CurrentRobotAccount(ctx context.Context) *RobotAccount {
4647
type currentRobotAccountCtxKey struct{}
4748

4849
// WithAttestationContextFromRobotAccount Middleware that injects the current user to the context
49-
func WithAttestationContextFromRobotAccount(robotAccountUseCase *biz.RobotAccountUseCase, logger *log.Helper) middleware.Middleware {
50+
func WithAttestationContextFromRobotAccount(robotAccountUseCase *biz.RobotAccountUseCase, orgUseCase *biz.OrganizationUseCase, logger *log.Helper) middleware.Middleware {
5051
return func(handler middleware.Handler) middleware.Handler {
5152
return func(ctx context.Context, req interface{}) (interface{}, error) {
5253
authInfo, ok := attjwtmiddleware.FromJWTAuthContext(ctx)
@@ -104,6 +105,13 @@ func WithAttestationContextFromRobotAccount(robotAccountUseCase *biz.RobotAccoun
104105
return nil, errors.New("error retrieving the organization from the auth token")
105106
}
106107

108+
org, err := orgUseCase.FindByID(ctx, orgID)
109+
if err != nil {
110+
return nil, fmt.Errorf("error retrieving the organization: %w", err)
111+
}
112+
113+
ctx = WithCurrentOrg(ctx, &Org{Name: org.Name, ID: org.ID, CreatedAt: org.CreatedAt})
114+
107115
// Check that the encoded workflow ID is the one associated with the robot account
108116
// NOTE: This in theory should not be necessary since currently we allow a robot account to be attached to ONLY ONE workflowID
109117
if account.WorkflowID.String() != workflowID {

0 commit comments

Comments
 (0)