git.iko.soy/typeable/oauth2-proxy
on master help sign in
Merge branch 'master' into Issue#259-RedirectToHTTPS
commit9cecc01aac9b086720f3006244790bfb6d4b1886 authorJosh Michielsen <github@mickey.dev> dateThu 17 Oct 2019, 22:13 (7 years ago) parentbed033686977f7
4 files changed+91−1
CHANGELOG.md+2 docs/2_auth.md+2 providers/azure.go+65 providers/azure_test.go+22−1
CHANGELOG.md+2
@@ -5,6 +5,8 @@
55- [#227](https://github.com/pusher/oauth2_proxy/pull/227) Add Keycloak provider (@Ofinka)
66- [#273](https://github.com/pusher/oauth2_proxy/pull/273) Support Go 1.13 (@dio)
77- [#275](https://github.com/pusher/oauth2_proxy/pull/275) docker: build from debian buster (@syscll)
8+- [#258](https://github.com/pusher/oauth2_proxy/pull/258) Add IDToken for Azure provider
9+ - This PR adds the IDToken into the session for the Azure provider allowing requests to a backend to be identified as a specific user. As a consequence, if you are using a cookie to store the session the cookie will now exceed the 4kb size limit and be split into multiple cookies. This can cause problems when using nginx as a proxy, resulting in no cookie being passed at all. Either increase the proxy_buffer_size in nginx or implement the redis session storage (see https://pusher.github.io/oauth2_proxy/configuration#redis-storage)
810
911# v4.0.0
1012
docs/2_auth.md+2
@@ -81,6 +81,8 @@ Note: The user is checked against the group members list on initial authenticati
8181 --client-secret=<value from step 6>
8282```
8383
84+Note: When using the Azure Auth provider with nginx and the cookie session store you may find the cookie is too large and doesn't get passed through correctly. Increasing the proxy_buffer_size in nginx or implementing the [redis session storage](configuration#redis-storage) should resolve this.
85+
8486### Facebook Auth Provider
8587
86881. Create a new FB App from <https://developers.facebook.com/>
providers/azure.go+65
@@ -1,10 +1,14 @@
11package providers
22
33import (
4+ "bytes"
5+ "encoding/json"
46 "errors"
57 "fmt"
8+ "io/ioutil"
69 "net/http"
710 "net/url"
11+ "time"
812
913 "github.com/bitly/go-simplejson"
1014 "github.com/pusher/oauth2_proxy/pkg/apis/sessions"
@@ -65,6 +69,67 @@ func (p *AzureProvider) Configure(tenant string) {
6569 }
6670}
6771
72+func (p *AzureProvider) Redeem(redirectURL, code string) (s *sessions.SessionState, err error) {
73+ if code == "" {
74+ err = errors.New("missing code")
75+ return
76+ }
77+
78+ params := url.Values{}
79+ params.Add("redirect_uri", redirectURL)
80+ params.Add("client_id", p.ClientID)
81+ params.Add("client_secret", p.ClientSecret)
82+ params.Add("code", code)
83+ params.Add("grant_type", "authorization_code")
84+ if p.ProtectedResource != nil && p.ProtectedResource.String() != "" {
85+ params.Add("resource", p.ProtectedResource.String())
86+ }
87+
88+ var req *http.Request
89+ req, err = http.NewRequest("POST", p.RedeemURL.String(), bytes.NewBufferString(params.Encode()))
90+ if err != nil {
91+ return
92+ }
93+ req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
94+
95+ var resp *http.Response
96+ resp, err = http.DefaultClient.Do(req)
97+ if err != nil {
98+ return nil, err
99+ }
100+ var body []byte
101+ body, err = ioutil.ReadAll(resp.Body)
102+ resp.Body.Close()
103+ if err != nil {
104+ return
105+ }
106+
107+ if resp.StatusCode != 200 {
108+ err = fmt.Errorf("got %d from %q %s", resp.StatusCode, p.RedeemURL.String(), body)
109+ return
110+ }
111+
112+ var jsonResponse struct {
113+ AccessToken string `json:"access_token"`
114+ RefreshToken string `json:"refresh_token"`
115+ ExpiresOn int64 `json:"expires_on,string"`
116+ IDToken string `json:"id_token"`
117+ }
118+ err = json.Unmarshal(body, &jsonResponse)
119+ if err != nil {
120+ return
121+ }
122+
123+ s = &sessions.SessionState{
124+ AccessToken: jsonResponse.AccessToken,
125+ IDToken: jsonResponse.IDToken,
126+ CreatedAt: time.Now(),
127+ ExpiresOn: time.Unix(jsonResponse.ExpiresOn, 0),
128+ RefreshToken: jsonResponse.RefreshToken,
129+ }
130+ return
131+}
132+
68133func getAzureHeader(accessToken string) http.Header {
69134 header := make(http.Header)
70135 header.Set("Authorization", fmt.Sprintf("Bearer %s", accessToken))
providers/azure_test.go+22−1
@@ -5,6 +5,7 @@ import (
55 "net/http/httptest"
66 "net/url"
77 "testing"
8+ "time"
89
910 "github.com/pusher/oauth2_proxy/pkg/apis/sessions"
1011 "github.com/stretchr/testify/assert"
@@ -20,6 +21,7 @@ func testAzureProvider(hostname string) *AzureProvider {
2021 ValidateURL: &url.URL{},
2122 ProtectedResource: &url.URL{},
2223 Scope: ""})
24+
2325 if hostname != "" {
2426 updateURL(p.Data().LoginURL, hostname)
2527 updateURL(p.Data().RedeemURL, hostname)
@@ -111,8 +113,11 @@ func testAzureBackend(payload string) *httptest.Server {
111113
112114 return httptest.NewServer(http.HandlerFunc(
113115 func(w http.ResponseWriter, r *http.Request) {
114− if r.URL.Path != path || r.URL.RawQuery != query {
116+ if (r.URL.Path != path || r.URL.RawQuery != query) && r.Method != "POST" {
115117 w.WriteHeader(404)
118+ } else if r.Method == "POST" && r.Body != nil {
119+ w.WriteHeader(200)
120+ w.Write([]byte(payload))
116121 } else if r.Header.Get("Authorization") != "Bearer imaginary_access_token" {
117122 w.WriteHeader(403)
118123 } else {
@@ -199,3 +204,19 @@ func TestAzureProviderGetEmailAddressIncorrectOtherMails(t *testing.T) {
199204 assert.Equal(t, "type assertion to string failed", err.Error())
200205 assert.Equal(t, "", email)
201206}
207+
208+func TestAzureProviderRedeemReturnsIdToken(t *testing.T) {
209+ b := testAzureBackend(`{ "id_token": "testtoken1234", "expires_on": "1136239445", "refresh_token": "refresh1234" }`)
210+ defer b.Close()
211+ timestamp, err := time.Parse(time.RFC3339, "2006-01-02T22:04:05Z")
212+ assert.Equal(t, nil, err)
213+
214+ bURL, _ := url.Parse(b.URL)
215+ p := testAzureProvider(bURL.Host)
216+ p.Data().RedeemURL.Path = "/common/oauth2/token"
217+ s, err := p.Redeem("https://localhost", "1234")
218+ assert.Equal(t, nil, err)
219+ assert.Equal(t, "testtoken1234", s.IDToken)
220+ assert.Equal(t, timestamp, s.ExpiresOn.UTC())
221+ assert.Equal(t, "refresh1234", s.RefreshToken)
222+}