-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpages.go
More file actions
107 lines (83 loc) · 3.4 KB
/
Copy pathpages.go
File metadata and controls
107 lines (83 loc) · 3.4 KB
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
package main
import (
"crypto/sha256"
"fmt"
"html"
"github.com/pilcrowonpaper/go-json"
_ "embed"
)
func createUnexpectedErrorErrorPageHTML(requestId string) (string, [][]byte, [][]byte) {
pageTitle := "An unexpected error occurred | Basic auth example"
bodyHTML := `<h1>An unexpected error occurred</h1>
<p>Something went wrong. Please refresh the page or try again later.</p>`
pageHTML, pageScriptHashes, pageStylesheetHashes := createPageHTML(requestId, pageTitle, bodyHTML, "", "", "")
return pageHTML, pageScriptHashes, pageStylesheetHashes
}
//go:embed assets/base.css
var baseStylesheet string
//go:embed assets/base.js
var baseScript string
func createPageHTML(requestId string, title string, bodyHTML string, script string, stylesheet string, dataJSON string) (string, [][]byte, [][]byte) {
baseScriptHash := sha256.Sum256([]byte(baseScript))
scriptHash := sha256.Sum256([]byte(script))
scriptHashes := [][]byte{baseScriptHash[:], scriptHash[:]}
baseStylesheetHash := sha256.Sum256([]byte(baseStylesheet))
stylesheetHash := sha256.Sum256([]byte(stylesheet))
stylesheetHashes := [][]byte{baseStylesheetHash[:], stylesheetHash[:]}
htmlTemplate := `<html lang="en">
<head>
<title>%s</title>
<meta name="description" content="An example website that implements email address and password authentication." />
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<meta property="og:title" content="%s" />
<meta property="og:type" content="website" />
<meta property="og:locale" content="en_US" />
<meta property="og:site_name" content="Basic auth example" />
<meta property="og:description" content="An example website that implements email address and password authentication." />
<meta property="og:url" content="https://basic-example.auth.pilcrowonpaper.com" />
<meta property="og:image" content="https://pilcrowonpaper.com/pilcrow.jpeg" />
<meta name="twitter:card" content="summary">
<meta name="twitter:site" content="@pilcrowonpaper">
<link rel="icon" type="image/jpeg" href="https://pilcrowonpaper.com/pilcrow.jpeg">
<style>%s</style>
<style>%s</style>
</head>
<body>
<header>
<a id="home-link" href="/">Basic auth example</a>
</header>
<main>%s</main>
<footer>
<p>Created by <a href="https://pilcrowonpaper.com">pilcrow</a></p>
<p>Source code: <a href="https://github.com/pilcrowonpaper/basic-example.auth.pilcrowonpaper.com">github.com/pilcrowonpaper/basic-example.auth.pilcrowonpaper.com</a></p>
<p>Questions and support: <a href="mailto:examples@auth.pilcrowonpaper.com">examples@auth.pilcrowonpaper.com</a></p>
<p>Request ID: %s</p>
</footer>
</body>
<script id="data" type="application/json">%s</script>
<script>%s</script>
<script>%s</script>
</html>`
pageHTML := fmt.Sprintf(
htmlTemplate,
html.EscapeString(title),
html.EscapeString(title),
baseStylesheet,
stylesheet,
bodyHTML,
html.EscapeString(requestId),
dataJSON,
baseScript,
script,
)
return pageHTML, scriptHashes, stylesheetHashes
}
var htmlSafeJSONStringCharacterEscapingBehavior json.StringCharacterEscapingBehaviorInterface = htmlSafeJSONStringCharacterEscapingBehaviorStruct{}
type htmlSafeJSONStringCharacterEscapingBehaviorStruct struct{}
func (htmlSafeJSONStringCharacterEscapingBehaviorStruct) UseCharacter(r rune) bool {
return r != '<' && r != '>'
}
func (htmlSafeJSONStringCharacterEscapingBehaviorStruct) UseShorthandEscapeSequence(_ rune) bool {
return true
}