Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

186 rindas
4.6 KiB

  1. // Copyright 2018 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package jira
  5. import (
  6. "context"
  7. "encoding/base64"
  8. "encoding/json"
  9. "net/http"
  10. "net/http/httptest"
  11. "strings"
  12. "testing"
  13. "golang.org/x/oauth2"
  14. "golang.org/x/oauth2/jws"
  15. )
  16. func TestJWTFetch_JSONResponse(t *testing.T) {
  17. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  18. w.Header().Set("Content-Type", "application/json")
  19. w.Write([]byte(`{
  20. "access_token": "90d64460d14870c08c81352a05dedd3465940a7c",
  21. "token_type": "Bearer",
  22. "expires_in": 3600
  23. }`))
  24. }))
  25. defer ts.Close()
  26. conf := &Config{
  27. BaseURL: "https://my.app.com",
  28. Subject: "userkey",
  29. Config: oauth2.Config{
  30. ClientID: "super_secret_client_id",
  31. ClientSecret: "super_shared_secret",
  32. Scopes: []string{"read", "write"},
  33. Endpoint: oauth2.Endpoint{
  34. AuthURL: "https://example.com",
  35. TokenURL: ts.URL,
  36. },
  37. },
  38. }
  39. tok, err := conf.TokenSource(context.Background()).Token()
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. if !tok.Valid() {
  44. t.Errorf("got invalid token: %v", tok)
  45. }
  46. if got, want := tok.AccessToken, "90d64460d14870c08c81352a05dedd3465940a7c"; got != want {
  47. t.Errorf("access token = %q; want %q", got, want)
  48. }
  49. if got, want := tok.TokenType, "Bearer"; got != want {
  50. t.Errorf("token type = %q; want %q", got, want)
  51. }
  52. if got := tok.Expiry.IsZero(); got {
  53. t.Errorf("token expiry = %v, want none", got)
  54. }
  55. }
  56. func TestJWTFetch_BadResponse(t *testing.T) {
  57. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  58. w.Header().Set("Content-Type", "application/json")
  59. w.Write([]byte(`{"token_type": "Bearer"}`))
  60. }))
  61. defer ts.Close()
  62. conf := &Config{
  63. BaseURL: "https://my.app.com",
  64. Subject: "userkey",
  65. Config: oauth2.Config{
  66. ClientID: "super_secret_client_id",
  67. ClientSecret: "super_shared_secret",
  68. Scopes: []string{"read", "write"},
  69. Endpoint: oauth2.Endpoint{
  70. AuthURL: "https://example.com",
  71. TokenURL: ts.URL,
  72. },
  73. },
  74. }
  75. tok, err := conf.TokenSource(context.Background()).Token()
  76. if err != nil {
  77. t.Fatal(err)
  78. }
  79. if tok == nil {
  80. t.Fatalf("got nil token; want token")
  81. }
  82. if tok.Valid() {
  83. t.Errorf("got invalid token: %v", tok)
  84. }
  85. if got, want := tok.AccessToken, ""; got != want {
  86. t.Errorf("access token = %q; want %q", got, want)
  87. }
  88. if got, want := tok.TokenType, "Bearer"; got != want {
  89. t.Errorf("token type = %q; want %q", got, want)
  90. }
  91. }
  92. func TestJWTFetch_BadResponseType(t *testing.T) {
  93. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  94. w.Header().Set("Content-Type", "application/json")
  95. w.Write([]byte(`{"access_token":123, "token_type": "Bearer"}`))
  96. }))
  97. defer ts.Close()
  98. conf := &Config{
  99. BaseURL: "https://my.app.com",
  100. Subject: "userkey",
  101. Config: oauth2.Config{
  102. ClientID: "super_secret_client_id",
  103. ClientSecret: "super_shared_secret",
  104. Endpoint: oauth2.Endpoint{
  105. AuthURL: "https://example.com",
  106. TokenURL: ts.URL,
  107. },
  108. },
  109. }
  110. tok, err := conf.TokenSource(context.Background()).Token()
  111. if err == nil {
  112. t.Error("got a token; expected error")
  113. if got, want := tok.AccessToken, ""; got != want {
  114. t.Errorf("access token = %q; want %q", got, want)
  115. }
  116. }
  117. }
  118. func TestJWTFetch_Assertion(t *testing.T) {
  119. var assertion string
  120. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  121. r.ParseForm()
  122. assertion = r.Form.Get("assertion")
  123. w.Header().Set("Content-Type", "application/json")
  124. w.Write([]byte(`{
  125. "access_token": "90d64460d14870c08c81352a05dedd3465940a7c",
  126. "token_type": "Bearer",
  127. "expires_in": 3600
  128. }`))
  129. }))
  130. defer ts.Close()
  131. conf := &Config{
  132. BaseURL: "https://my.app.com",
  133. Subject: "userkey",
  134. Config: oauth2.Config{
  135. ClientID: "super_secret_client_id",
  136. ClientSecret: "super_shared_secret",
  137. Endpoint: oauth2.Endpoint{
  138. AuthURL: "https://example.com",
  139. TokenURL: ts.URL,
  140. },
  141. },
  142. }
  143. _, err := conf.TokenSource(context.Background()).Token()
  144. if err != nil {
  145. t.Fatalf("Failed to fetch token: %v", err)
  146. }
  147. parts := strings.Split(assertion, ".")
  148. if len(parts) != 3 {
  149. t.Fatalf("assertion = %q; want 3 parts", assertion)
  150. }
  151. gotjson, err := base64.RawURLEncoding.DecodeString(parts[0])
  152. if err != nil {
  153. t.Fatalf("invalid token header; err = %v", err)
  154. }
  155. got := jws.Header{}
  156. if err := json.Unmarshal(gotjson, &got); err != nil {
  157. t.Errorf("failed to unmarshal json token header = %q; err = %v", gotjson, err)
  158. }
  159. want := jws.Header{
  160. Algorithm: "HS256",
  161. Typ: "JWT",
  162. }
  163. if got != want {
  164. t.Errorf("access token header = %q; want %q", got, want)
  165. }
  166. }