Skip to content

Commit e8632fa

Browse files
author
thisisaaronland
committed
snapshot with append access token as data attribute handler
1 parent 58d7f60 commit e8632fa

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ go 1.12
55
require (
66
github.com/aaronland/go-http-cookie v0.3.2
77
github.com/aaronland/go-http-crumb v0.1.1
8+
github.com/aaronland/go-http-rewrite v0.0.5
89
github.com/aaronland/go-http-sanitize v0.0.4
910
github.com/aaronland/go-string v0.1.2
1011
github.com/awnumar/memguard v0.22.2
1112
github.com/sfomuseum/go-flags v0.2.1
13+
golang.org/x/net v0.0.0-20200519113804-d87ec0cfa476
1214
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
1315
)

www/cookie.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ import (
55
"encoding/json"
66
"errors"
77
"github.com/aaronland/go-http-cookie"
8+
"github.com/aaronland/go-http-rewrite"
89
"github.com/aaronland/go-http-sanitize"
910
"github.com/awnumar/memguard"
1011
"github.com/sfomuseum/go-http-oauth2"
12+
"golang.org/x/net/html"
1113
goog_oauth2 "golang.org/x/oauth2"
14+
"io"
1215
_ "log"
1316
"net/http"
1417
"net/url"
@@ -273,6 +276,46 @@ func OAuth2RemoveAccessTokenCookieHandler(opts *oauth2.Options) (http.Handler, e
273276
return h, nil
274277
}
275278

279+
func AppendAccessTokenHandler(opts *oauth2.Options, next_handler http.Handler) http.Handler {
280+
281+
fn := func(rsp http.ResponseWriter, req *http.Request) {
282+
283+
token, err := GetOAuth2TokenFromCookie(opts, req)
284+
285+
if err != nil {
286+
// http.Error(rsp, err.Error(), http.StatusInternalServerError)
287+
return
288+
}
289+
290+
rewrite_func := NewAccessTokenRewriteFunc(token)
291+
rewrite_handler := rewrite.RewriteHTMLHandler(next_handler, rewrite_func)
292+
293+
rewrite_handler.ServeHTTP(rsp, req)
294+
}
295+
296+
return http.HandlerFunc(fn)
297+
}
298+
299+
func NewAccessTokenRewriteFunc(token *goog_oauth2.Token) rewrite.RewriteHTMLFunc {
300+
301+
var rewrite_func rewrite.RewriteHTMLFunc
302+
303+
rewrite_func = func(n *html.Node, w io.Writer) {
304+
305+
if n.Type == html.ElementNode && n.Data == "body" {
306+
307+
token_ns := ""
308+
token_key := "data-access-token"
309+
token_value := token.AccessToken
310+
311+
token_attr := html.Attribute{token_ns, token_key, token_value}
312+
n.Attr = append(n.Attr, token_attr)
313+
}
314+
}
315+
316+
return rewrite_func
317+
}
318+
276319
func GetOAuth2TokenFromCookie(opts *oauth2.Options, req *http.Request) (*goog_oauth2.Token, error) {
277320

278321
ctx := req.Context()

0 commit comments

Comments
 (0)