Skip to content

Commit 7c6d73f

Browse files
authored
Merge pull request #22767 from enj/enj/i/frame_mux
Bug 1693018: Add CSRF to token request endpoint
2 parents d5d8b2c + 9d69335 commit 7c6d73f

8 files changed

Lines changed: 263 additions & 121 deletions

File tree

pkg/oauthserver/mux.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package oauthserver
22

3-
import (
4-
"net/http"
5-
)
3+
import "net/http"
64

75
type Mux interface {
86
Handle(pattern string, handler http.Handler)
97
HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))
108
}
9+
10+
type Endpoints interface {
11+
// Install registers one or more http.Handlers into the given mux.
12+
// It is expected that the provided prefix will serve all operations.
13+
// prefix MUST NOT end in a slash.
14+
Install(mux Mux, prefix string)
15+
}

pkg/oauthserver/oauthserver/auth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func (c *OAuthServerConfig) WithOAuth(handler http.Handler) (http.Handler, error
149149
loginURL = c.ExtraOAuthConfig.Options.MasterPublicURL
150150
}
151151

152-
tokenRequestEndpoints := tokenrequest.NewEndpoints(loginURL, openShiftLogoutPrefix, c.getOsinOAuthClient, c.ExtraOAuthConfig.OAuthAccessTokenClient)
152+
tokenRequestEndpoints := tokenrequest.NewTokenRequest(loginURL, openShiftLogoutPrefix, c.getOsinOAuthClient, c.ExtraOAuthConfig.OAuthAccessTokenClient, c.getCSRF())
153153
tokenRequestEndpoints.Install(mux, urls.OpenShiftOAuthAPIPrefix)
154154

155155
if session := c.ExtraOAuthConfig.SessionAuth; session != nil {

pkg/oauthserver/osinserver/osinserver.go

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"github.com/openshift/origin/pkg/oauthserver"
1515
)
1616

17-
type Server struct {
17+
type osinServer struct {
1818
config *osin.ServerConfig
1919
server *osin.Server
2020
authorize AuthorizeHandler
@@ -31,15 +31,15 @@ func (l Logger) Printf(format string, v ...interface{}) {
3131
}
3232
}
3333

34-
func New(config *osin.ServerConfig, storage osin.Storage, authorize AuthorizeHandler, access AccessHandler, errorHandler ErrorHandler) *Server {
34+
func New(config *osin.ServerConfig, storage osin.Storage, authorize AuthorizeHandler, access AccessHandler, errorHandler ErrorHandler) oauthserver.Endpoints {
3535
server := osin.NewServer(config, storage)
3636

3737
// Override tokengen to ensure we get valid length tokens
3838
server.AuthorizeTokenGen = TokenGen{}
3939
server.AccessTokenGen = TokenGen{}
4040
server.Logger = Logger{}
4141

42-
return &Server{
42+
return &osinServer{
4343
config: config,
4444
server: server,
4545
authorize: authorize,
@@ -48,17 +48,13 @@ func New(config *osin.ServerConfig, storage osin.Storage, authorize AuthorizeHan
4848
}
4949
}
5050

51-
// Install registers the Server OAuth handlers into a mux. It is expected that the
52-
// provided prefix will serve all operations
53-
func (s *Server) Install(mux oauthserver.Mux, paths ...string) {
54-
for _, prefix := range paths {
55-
mux.HandleFunc(path.Join(prefix, urls.AuthorizePath), s.handleAuthorize)
56-
mux.HandleFunc(path.Join(prefix, urls.TokenPath), s.handleToken)
57-
mux.HandleFunc(path.Join(prefix, urls.InfoPath), s.handleInfo)
58-
}
51+
func (s *osinServer) Install(mux oauthserver.Mux, prefix string) {
52+
mux.HandleFunc(path.Join(prefix, urls.AuthorizePath), s.handleAuthorize)
53+
mux.HandleFunc(path.Join(prefix, urls.TokenPath), s.handleToken)
54+
mux.HandleFunc(path.Join(prefix, urls.InfoPath), s.handleInfo)
5955
}
6056

61-
func (s *Server) handleAuthorize(w http.ResponseWriter, r *http.Request) {
57+
func (s *osinServer) handleAuthorize(w http.ResponseWriter, r *http.Request) {
6258
resp := s.server.NewResponse()
6359
defer resp.Close()
6460

@@ -97,7 +93,7 @@ func (s *Server) handleAuthorize(w http.ResponseWriter, r *http.Request) {
9793
osin.OutputJSON(resp, w, r)
9894
}
9995

100-
func (s *Server) handleToken(w http.ResponseWriter, r *http.Request) {
96+
func (s *osinServer) handleToken(w http.ResponseWriter, r *http.Request) {
10197
resp := s.server.NewResponse()
10298
defer resp.Close()
10399

@@ -114,7 +110,7 @@ func (s *Server) handleToken(w http.ResponseWriter, r *http.Request) {
114110
osin.OutputJSON(resp, w, r)
115111
}
116112

117-
func (s *Server) handleInfo(w http.ResponseWriter, r *http.Request) {
113+
func (s *osinServer) handleInfo(w http.ResponseWriter, r *http.Request) {
118114
resp := s.server.NewResponse()
119115
defer resp.Close()
120116

pkg/oauthserver/server/grant/grant.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"net/http"
66
"net/url"
77
"path"
8-
"strings"
98

109
"k8s.io/klog"
1110

@@ -98,13 +97,8 @@ func NewGrant(csrf csrf.CSRF, auth authenticator.Request, render FormRenderer, c
9897
}
9998
}
10099

101-
// Install registers the grant handler into a mux. It is expected that the
102-
// provided prefix will serve all operations. Path MUST NOT end in a slash.
103-
func (l *Grant) Install(mux oauthserver.Mux, paths ...string) {
104-
for _, path := range paths {
105-
path = strings.TrimRight(path, "/")
106-
mux.HandleFunc(path, l.ServeHTTP)
107-
}
100+
func (l *Grant) Install(mux oauthserver.Mux, prefix string) {
101+
mux.Handle(prefix, l)
108102
}
109103

110104
func (l *Grant) ServeHTTP(w http.ResponseWriter, req *http.Request) {

pkg/oauthserver/server/login/login.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"fmt"
88
"html/template"
99
"net/http"
10-
"strings"
1110

1211
"k8s.io/klog"
1312

@@ -89,13 +88,8 @@ func NewLogin(provider string, csrf csrf.CSRF, auth PasswordAuthenticator, rende
8988
}
9089
}
9190

92-
// Install registers the login handler into a mux. It is expected that the
93-
// provided prefix will serve all operations. Path MUST NOT end in a slash.
94-
func (l *Login) Install(mux oauthserver.Mux, paths ...string) {
95-
for _, path := range paths {
96-
path = strings.TrimRight(path, "/")
97-
mux.HandleFunc(path, l.ServeHTTP)
98-
}
91+
func (l *Login) Install(mux oauthserver.Mux, prefix string) {
92+
mux.Handle(prefix, l)
9993
}
10094

10195
func (l *Login) ServeHTTP(w http.ResponseWriter, req *http.Request) {

pkg/oauthserver/server/logout/logout.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ import (
1111
"github.com/openshift/origin/pkg/oauthserver"
1212
"github.com/openshift/origin/pkg/oauthserver/server/redirect"
1313
"github.com/openshift/origin/pkg/oauthserver/server/session"
14-
"github.com/openshift/origin/pkg/oauthserver/server/tokenrequest"
1514
)
1615

1716
const thenParam = "then"
1817

19-
func NewLogout(invalidator session.SessionInvalidator, redirect string) tokenrequest.Endpoints {
18+
func NewLogout(invalidator session.SessionInvalidator, redirect string) oauthserver.Endpoints {
2019
return &logout{
2120
invalidator: invalidator,
2221
redirect: redirect,
@@ -28,10 +27,8 @@ type logout struct {
2827
redirect string
2928
}
3029

31-
func (l *logout) Install(mux oauthserver.Mux, paths ...string) {
32-
for _, path := range paths {
33-
mux.Handle(path, l)
34-
}
30+
func (l *logout) Install(mux oauthserver.Mux, prefix string) {
31+
mux.Handle(prefix, l)
3532
}
3633

3734
func (l *logout) ServeHTTP(w http.ResponseWriter, req *http.Request) {

0 commit comments

Comments
 (0)