Skip to content

Commit 0244ad3

Browse files
committed
Set nhp-token cookie
1 parent ea7874e commit 0244ad3

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

common/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type ResourceData struct {
3232
RedirectWithParams bool `json:"redirectWithParams,omitempty"`
3333
SkipAuth bool `json:"skipAuth,omitempty"`
3434
CookieDomain string `json:"cookieDomain,omitempty"`
35+
AccessControlAllowOrigin string `json:accessControlAllowOrigin",omitempty"`
3536
}
3637

3738
type ResourceGroupMap map[string]*ResourceData

server/plugins/example/etc/resource.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
# OpenTime: seconds for traffic passing duration after successful knock.
77
# RedirectUrl: a customized url send back with the http response message as an option for redirection. (only applicable for http agent)
88
# RedirectWithParams: whether or not to include queries in the original http request. (only applicable for http agent)
9+
# AccessControlAllowOrigin: the response header indicates whether the response can be shared with requesting code from the given origin.
910
["demo"]
1011
SkipAuth = true
1112
OpenTime = 15
1213
RedirectUrl = "https://acdemo.opennhp.org"
1314
RedirectWithParams = false
1415
CookieDomain = "opennhp.org"
16+
AccessControlAllowOrigin = "https://demologin.opennhp.org"
1517

1618
# syntax ["{ResourceId}".Resources."{ResourceName}"]
1719
# ResourceName: name of resource inside a resource group. Each ResourceId can have multiple ResourceNames.

server/plugins/example/main.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ func AuthWithHttp(ctx *gin.Context, req *common.HttpKnockRequest, helper *plugin
186186
return
187187
}
188188

189+
corsMiddleware(ctx, res.AccessControlAllowOrigin)
190+
189191
switch {
190192
case strings.EqualFold(action, "valid"):
191193
ackMsg, err = authRegular(ctx, req, res, helper)
@@ -316,6 +318,28 @@ func AuthWithNHP(req *common.NhpAuthRequest, helper *plugins.NhpServerPluginHelp
316318
return ackMsg, err
317319
}
318320

321+
func corsMiddleware(ctx *gin.Context, originResource string) {
322+
// HTTP headers for CORS
323+
ctx.Writer.Header().Set("Access-Control-Allow-Origin", originResource) // allow cross-origin resource sharing
324+
ctx.Writer.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS, POST") // methods
325+
ctx.Writer.Header().Set("Access-Control-Expose-Headers", "Content-Type, Content-Length, Set-Cookie")
326+
ctx.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Authorization, X-NHP-Ver, Cookie")
327+
ctx.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
328+
ctx.Writer.Header().Set("Access-Control-Max-Age", "300")
329+
330+
if ctx.Request.Method == "OPTIONS" {
331+
ctx.Status(http.StatusOK)
332+
return
333+
}
334+
335+
if ctx.Request.Method == "DELETE" || ctx.Request.Method == "PUT" {
336+
ctx.AbortWithStatus(http.StatusNoContent)
337+
return
338+
}
339+
340+
ctx.Next()
341+
}
342+
319343
func main() {
320344

321345
}

server/plugins/example/templates/example_login.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,9 @@ <h2 id="authSuccessMessage"></h2>
289289
"&password=" + encodeURIComponent(password);
290290
console.log(nhpValidUrl);
291291

292-
fetch(nhpValidUrl)
292+
fetch(nhpValidUrl,{
293+
credentials: "include"
294+
})
293295
.then(response => response.json())
294296
.then(result => {
295297
console.log(result);

0 commit comments

Comments
 (0)