mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06: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:
parent
eab7990349
commit
4af5aef417
@ -28,8 +28,8 @@ type ForwardIDMiddleware struct {
|
||||
|
||||
func (m *ForwardIDMiddleware) applyToken(ctx context.Context, pCtx backend.PluginContext, req backend.ForwardHTTPHeaders) error {
|
||||
reqCtx := contexthandler.FromContext(ctx)
|
||||
// if request not for a datasource or no HTTP request context skip middleware
|
||||
if req == nil || reqCtx == nil || reqCtx.SignedInUser == nil || pCtx.DataSourceInstanceSettings == nil {
|
||||
// no HTTP request context => skip middleware
|
||||
if req == nil || reqCtx == nil || reqCtx.SignedInUser == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -51,4 +51,24 @@ func TestForwardIDMiddleware(t *testing.T) {
|
||||
|
||||
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])
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user