/
osinserver.go
125 lines (102 loc) · 3.21 KB
/
osinserver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package osinserver
import (
"fmt"
"net/http"
"path"
"github.com/RangelReale/osin"
"k8s.io/klog"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"github.com/openshift/origin/pkg/oauth/urls"
"github.com/openshift/origin/pkg/oauthserver"
)
type Server struct {
config *osin.ServerConfig
server *osin.Server
authorize AuthorizeHandler
access AccessHandler
errorHandler ErrorHandler
}
// Logger captures additional osin server errors
type Logger struct{}
func (l Logger) Printf(format string, v ...interface{}) {
if klog.V(2) {
klog.ErrorDepth(3, fmt.Sprintf("osin: "+format, v...))
}
}
func New(config *osin.ServerConfig, storage osin.Storage, authorize AuthorizeHandler, access AccessHandler, errorHandler ErrorHandler) *Server {
server := osin.NewServer(config, storage)
// Override tokengen to ensure we get valid length tokens
server.AuthorizeTokenGen = TokenGen{}
server.AccessTokenGen = TokenGen{}
server.Logger = Logger{}
return &Server{
config: config,
server: server,
authorize: authorize,
access: access,
errorHandler: errorHandler,
}
}
// Install registers the Server OAuth handlers into a mux. It is expected that the
// provided prefix will serve all operations
func (s *Server) Install(mux oauthserver.Mux, paths ...string) {
for _, prefix := range paths {
mux.HandleFunc(path.Join(prefix, urls.AuthorizePath), s.handleAuthorize)
mux.HandleFunc(path.Join(prefix, urls.TokenPath), s.handleToken)
mux.HandleFunc(path.Join(prefix, urls.InfoPath), s.handleInfo)
}
}
func (s *Server) handleAuthorize(w http.ResponseWriter, r *http.Request) {
resp := s.server.NewResponse()
defer resp.Close()
if ar := s.server.HandleAuthorizeRequest(resp, r); ar != nil {
if errorCode := r.FormValue("error"); len(errorCode) != 0 {
// The request already has an error parameter, return directly to the user
resp.SetErrorUri(
r.FormValue("error"),
r.FormValue("error_description"),
r.FormValue("error_uri"),
r.FormValue("state"),
)
// force redirect response
resp.SetRedirect(ar.RedirectUri)
} else {
handled, err := s.authorize.HandleAuthorize(ar, resp, w)
if err != nil {
s.errorHandler.HandleError(err, w, r)
return
}
if handled {
return
}
s.server.FinishAuthorizeRequest(resp, r, ar)
}
}
if resp.IsError && resp.InternalError != nil {
utilruntime.HandleError(fmt.Errorf("internal error: %s", resp.InternalError))
}
osin.OutputJSON(resp, w, r)
}
func (s *Server) handleToken(w http.ResponseWriter, r *http.Request) {
resp := s.server.NewResponse()
defer resp.Close()
if ar := s.server.HandleAccessRequest(resp, r); ar != nil {
if err := s.access.HandleAccess(ar, w); err != nil {
s.errorHandler.HandleError(err, w, r)
return
}
s.server.FinishAccessRequest(resp, r, ar)
}
if resp.IsError && resp.InternalError != nil {
utilruntime.HandleError(fmt.Errorf("internal error: %s", resp.InternalError))
}
osin.OutputJSON(resp, w, r)
}
func (s *Server) handleInfo(w http.ResponseWriter, r *http.Request) {
resp := s.server.NewResponse()
defer resp.Close()
if ir := s.server.HandleInfoRequest(resp, r); ir != nil {
s.server.FinishInfoRequest(resp, r, ir)
}
osin.OutputJSON(resp, w, r)
}