@@ -2,27 +2,57 @@ package fake
22
33import (
44 "context"
5+ "encoding/json"
56 "net/http"
67 "net/http/httptest"
8+ "sync"
9+ "time"
710
11+ "github.com/Venafi/vcert/v4/test/tpp/fake/models"
812 "github.com/go-logr/logr"
913)
1014
11- type Fake struct {
12- * httptest.Server
15+ type state struct {
16+ sync.RWMutex
17+ username string
18+ password string
19+ refreshToken string
20+ accessToken string
21+ refreshTokenExpires time.Time
1322}
1423
15- func New () * Fake {
16- mux := http .NewServeMux ()
17- mux .HandleFunc ("/vedauth/authorize/oauth" , func (w http.ResponseWriter , req * http.Request ) {
18- defer req .Body .Close ()
19- w .Write ([]byte ("{}" ))
20- return
21- })
22- ts := httptest .NewUnstartedServer (mux )
23- return & Fake {
24- Server : ts ,
25- }
24+ func (o * state ) WithUsername (username string ) * state {
25+ o .Lock ()
26+ defer o .Unlock ()
27+ o .username = username
28+ return o
29+ }
30+
31+ func (o * state ) WithPassword (password string ) * state {
32+ o .Lock ()
33+ defer o .Unlock ()
34+ o .password = password
35+ return o
36+ }
37+
38+ func (o * state ) WithRefreshToken (token string ) * state {
39+ o .Lock ()
40+ defer o .Unlock ()
41+ o .accessToken = token
42+ return o
43+ }
44+
45+ func (o * state ) WithAccessToken (token string ) * state {
46+ o .Lock ()
47+ defer o .Unlock ()
48+ o .accessToken = token
49+ return o
50+ }
51+
52+ type Fake struct {
53+ * state
54+ * httptest.Server
55+ log logr.Logger
2656}
2757
2858func (o * Fake ) Start (ctx context.Context ) {
@@ -37,6 +67,105 @@ func (o *Fake) Close(ctx context.Context) {
3767 o .Server .Close ()
3868}
3969
70+ func New (log logr.Logger ) * Fake {
71+ mux := http .NewServeMux ()
72+ ts := httptest .NewUnstartedServer (mux )
73+ f := & Fake {
74+ log : log ,
75+ state : & state {},
76+ Server : ts ,
77+ }
78+ mux .HandleFunc ("/vedauth/authorize/oauth" , f .handlerAuthorizeOAuth )
79+ mux .HandleFunc ("/vedsdk/Identity/Self" , f .handlerIdentitySelf )
80+ mux .HandleFunc ("/vedsdk/certificates/checkpolicy" , f .handlerCertificatesCheckPolicy )
81+ mux .HandleFunc ("/vedsdk/" , f .handlerPing )
82+ mux .HandleFunc ("/" , f .handlerCatchAll )
83+ return f
84+ }
85+
86+ func (o * Fake ) handlerAuthorizeOAuth (w http.ResponseWriter , req * http.Request ) {
87+ defer req .Body .Close ()
88+ o .log .Info ("request" , "uri" , req .RequestURI )
89+ decoder := json .NewDecoder (req .Body )
90+ var in models.AuthorizeOAuthRequest
91+ if err := decoder .Decode (& in ); err != nil {
92+ http .Error (w , err .Error (), http .StatusInternalServerError )
93+ return
94+ }
95+ if in .Username != o .username || in .Password != o .password {
96+ // Mimics the behavior of TPP 20.4 and above. See:
97+ // https://github.com/jetstack/venafi-oauth-helper/issues/25#issuecomment-854037706
98+ http .Error (w , `{"error":"invalid_grant","error_description":"Username\/password combination not valid"}` ,
99+ http .StatusBadRequest )
100+ return
101+ }
102+ o .WithRefreshToken (o .refreshToken + "x" )
103+ o .WithAccessToken (o .accessToken + "x" )
104+ out := models.AuthorizeOAuthResponse {
105+ AccessToken : o .accessToken ,
106+ Expires : uint64 (time .Now ().UTC ().Add (time .Hour ).Unix ()),
107+ RefreshToken : o .refreshToken ,
108+ RefreshUntil : uint64 (o .refreshTokenExpires .Unix ()),
109+ Scope : in .Scope ,
110+ TokenType : "Bearer" ,
111+ Identity : "" ,
112+ }
113+ encoder := json .NewEncoder (w )
114+ if err := encoder .Encode (& out ); err != nil {
115+ http .Error (w , err .Error (), http .StatusInternalServerError )
116+ return
117+ }
118+ }
119+
120+ func (o * Fake ) handlerIdentitySelf (w http.ResponseWriter , req * http.Request ) {
121+ defer req .Body .Close ()
122+ log := o .log .WithValues ("uri" , req .RequestURI ).WithName ("handlerIdentifySelf" )
123+ log .V (1 ).Info ("request" )
124+ out := models.IdentityWebResponse {
125+ Identities : []* models.IdentityEntry {
126+ & models.IdentityEntry {
127+ Name : "Joe Bloggs" ,
128+ },
129+ },
130+ }
131+ encoder := json .NewEncoder (w )
132+ if err := encoder .Encode (& out ); err != nil {
133+ log .Error (err , "While encoding response" )
134+ http .Error (w , err .Error (), http .StatusInternalServerError )
135+ return
136+ }
137+ }
138+
139+ func (o * Fake ) handlerCertificatesCheckPolicy (w http.ResponseWriter , req * http.Request ) {
140+ defer req .Body .Close ()
141+ log := o .log .WithValues ("uri" , req .RequestURI ).WithName ("handlerCertificatesCheckPolicy" )
142+ log .V (1 ).Info ("request" )
143+ decoder := json .NewDecoder (req .Body )
144+ var in models.CheckPolicyRequest
145+ if err := decoder .Decode (& in ); err != nil {
146+ http .Error (w , err .Error (), http .StatusInternalServerError )
147+ return
148+ }
149+ out := models.CheckPolicyResponse {}
150+ encoder := json .NewEncoder (w )
151+ if err := encoder .Encode (& out ); err != nil {
152+ http .Error (w , err .Error (), http .StatusInternalServerError )
153+ return
154+ }
155+ }
156+
157+ func (o * Fake ) handlerPing (w http.ResponseWriter , req * http.Request ) {
158+ defer req .Body .Close ()
159+ o .log .Info ("request" , "uri" , req .RequestURI )
160+ if req .URL .Path != "/vedsdk/" {
161+ panic (req )
162+ }
163+ }
164+
165+ func (o * Fake ) handlerCatchAll (w http.ResponseWriter , req * http.Request ) {
166+ panic (req )
167+ }
168+
40169func logFromContext (ctx context.Context ) logr.Logger {
41170 log , err := logr .FromContext (ctx )
42171 if err != nil {
0 commit comments