access_token_provider_test.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package pluginproxy
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. "github.com/grafana/grafana/pkg/plugins"
  7. . "github.com/smartystreets/goconvey/convey"
  8. "golang.org/x/oauth2"
  9. "golang.org/x/oauth2/jwt"
  10. )
  11. func TestAccessToken(t *testing.T) {
  12. Convey("Plugin with JWT token auth route", t, func() {
  13. pluginRoute := &plugins.AppPluginRoute{
  14. Path: "pathwithjwttoken1",
  15. Url: "https://api.jwt.io/some/path",
  16. Method: "GET",
  17. JwtTokenAuth: &plugins.JwtTokenAuth{
  18. Url: "https://login.server.com/{{.JsonData.tenantId}}/oauth2/token",
  19. Scopes: []string{
  20. "https://www.testapi.com/auth/monitoring.read",
  21. "https://www.testapi.com/auth/cloudplatformprojects.readonly",
  22. },
  23. Params: map[string]string{
  24. "token_uri": "{{.JsonData.tokenUri}}",
  25. "client_email": "{{.JsonData.clientEmail}}",
  26. "private_key": "{{.SecureJsonData.privateKey}}",
  27. },
  28. },
  29. }
  30. templateData := templateData{
  31. JsonData: map[string]interface{}{
  32. "clientEmail": "test@test.com",
  33. "tokenUri": "login.url.com/token",
  34. },
  35. SecureJsonData: map[string]string{
  36. "privateKey": "testkey",
  37. },
  38. }
  39. Convey("should fetch token using jwt private key", func() {
  40. getTokenSource = func(conf *jwt.Config, ctx context.Context) (*oauth2.Token, error) {
  41. return &oauth2.Token{AccessToken: "abc"}, nil
  42. }
  43. provider := newAccessTokenProvider(1, pluginRoute)
  44. token, err := provider.getJwtAccessToken(context.Background(), templateData)
  45. So(err, ShouldBeNil)
  46. So(token, ShouldEqual, "abc")
  47. })
  48. Convey("should set jwt config values", func() {
  49. getTokenSource = func(conf *jwt.Config, ctx context.Context) (*oauth2.Token, error) {
  50. So(conf.Email, ShouldEqual, "test@test.com")
  51. So(conf.PrivateKey, ShouldResemble, []byte("testkey"))
  52. So(len(conf.Scopes), ShouldEqual, 2)
  53. So(conf.Scopes[0], ShouldEqual, "https://www.testapi.com/auth/monitoring.read")
  54. So(conf.Scopes[1], ShouldEqual, "https://www.testapi.com/auth/cloudplatformprojects.readonly")
  55. So(conf.TokenURL, ShouldEqual, "login.url.com/token")
  56. return &oauth2.Token{AccessToken: "abc"}, nil
  57. }
  58. provider := newAccessTokenProvider(1, pluginRoute)
  59. _, err := provider.getJwtAccessToken(context.Background(), templateData)
  60. So(err, ShouldBeNil)
  61. })
  62. Convey("should use cached token on second call", func() {
  63. getTokenSource = func(conf *jwt.Config, ctx context.Context) (*oauth2.Token, error) {
  64. return &oauth2.Token{
  65. AccessToken: "abc",
  66. Expiry: time.Now().Add(1 * time.Minute)}, nil
  67. }
  68. provider := newAccessTokenProvider(1, pluginRoute)
  69. token1, err := provider.getJwtAccessToken(context.Background(), templateData)
  70. So(err, ShouldBeNil)
  71. So(token1, ShouldEqual, "abc")
  72. getTokenSource = func(conf *jwt.Config, ctx context.Context) (*oauth2.Token, error) {
  73. return &oauth2.Token{AccessToken: "error: cache not used"}, nil
  74. }
  75. token2, err := provider.getJwtAccessToken(context.Background(), templateData)
  76. So(err, ShouldBeNil)
  77. So(token2, ShouldEqual, "abc")
  78. })
  79. })
  80. }