mirror of
https://github.com/grafana/grafana.git
synced 2025-08-01 23:53:20 +08:00
id forwarding: transfer Grafana id token to app plugins (#81967)
* id forwarding: allow for app plugins as well * Add test
This commit is contained in:
@ -28,8 +28,8 @@ type ForwardIDMiddleware struct {
|
|||||||
|
|
||||||
func (m *ForwardIDMiddleware) applyToken(ctx context.Context, pCtx backend.PluginContext, req backend.ForwardHTTPHeaders) error {
|
func (m *ForwardIDMiddleware) applyToken(ctx context.Context, pCtx backend.PluginContext, req backend.ForwardHTTPHeaders) error {
|
||||||
reqCtx := contexthandler.FromContext(ctx)
|
reqCtx := contexthandler.FromContext(ctx)
|
||||||
// if request not for a datasource or no HTTP request context skip middleware
|
// no HTTP request context => skip middleware
|
||||||
if req == nil || reqCtx == nil || reqCtx.SignedInUser == nil || pCtx.DataSourceInstanceSettings == nil {
|
if req == nil || reqCtx == nil || reqCtx.SignedInUser == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -51,4 +51,24 @@ func TestForwardIDMiddleware(t *testing.T) {
|
|||||||
|
|
||||||
require.Len(t, cdt.CallResourceReq.Headers[forwardIDHeaderName], 0)
|
require.Len(t, cdt.CallResourceReq.Headers[forwardIDHeaderName], 0)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
pluginContext = backend.PluginContext{
|
||||||
|
AppInstanceSettings: &backend.AppInstanceSettings{},
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Run("Should set forwarded id header to app plugin if present", func(t *testing.T) {
|
||||||
|
cdt := clienttest.NewClientDecoratorTest(t, clienttest.WithMiddlewares(NewForwardIDMiddleware()))
|
||||||
|
|
||||||
|
ctx := context.WithValue(context.Background(), ctxkey.Key{}, &contextmodel.ReqContext{
|
||||||
|
Context: &web.Context{Req: &http.Request{}},
|
||||||
|
SignedInUser: &user.SignedInUser{IDToken: "some-token"},
|
||||||
|
})
|
||||||
|
|
||||||
|
err := cdt.Decorator.CallResource(ctx, &backend.CallResourceRequest{
|
||||||
|
PluginContext: pluginContext,
|
||||||
|
}, nopCallResourceSender)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
require.Equal(t, "some-token", cdt.CallResourceReq.Headers[forwardIDHeaderName][0])
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user