Skip to content

Commit

Permalink
Set nhp-token cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
jerry-0824 committed Nov 4, 2024
1 parent ea7874e commit 84db690
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type ResourceData struct {
RedirectWithParams bool `json:"redirectWithParams,omitempty"`
SkipAuth bool `json:"skipAuth,omitempty"`
CookieDomain string `json:"cookieDomain,omitempty"`
AccessControlAllowOrigin string `json:accessControlAllowOrigin",omitempty"`
}

type ResourceGroupMap map[string]*ResourceData
Expand Down
2 changes: 2 additions & 0 deletions server/plugins/example/etc/resource.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
# OpenTime: seconds for traffic passing duration after successful knock.
# RedirectUrl: a customized url send back with the http response message as an option for redirection. (only applicable for http agent)
# RedirectWithParams: whether or not to include queries in the original http request. (only applicable for http agent)
# AccessControlAllowOrigin: the response header indicates whether the response can be shared with requesting code from the given origin.
["demo"]
SkipAuth = true
OpenTime = 15
RedirectUrl = "https://acdemo.opennhp.org"
RedirectWithParams = false
CookieDomain = "opennhp.org"
AccessControlAllowOrigin = "https://demologin.opennhp.org"

# syntax ["{ResourceId}".Resources."{ResourceName}"]
# ResourceName: name of resource inside a resource group. Each ResourceId can have multiple ResourceNames.
Expand Down
9 changes: 9 additions & 0 deletions server/plugins/example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ func AuthWithHttp(ctx *gin.Context, req *common.HttpKnockRequest, helper *plugin
return
}

corsMiddleware(ctx, res.AccessControlAllowOrigin)

switch {
case strings.EqualFold(action, "valid"):
ackMsg, err = authRegular(ctx, req, res, helper)
Expand Down Expand Up @@ -316,6 +318,13 @@ func AuthWithNHP(req *common.NhpAuthRequest, helper *plugins.NhpServerPluginHelp
return ackMsg, err
}

func corsMiddleware(ctx *gin.Context, originResource string) {
// HTTP headers for CORS
ctx.Writer.Header().Set("Access-Control-Allow-Origin", originResource) // allow cross-origin resource sharing

ctx.Next()
}

func main() {

}
4 changes: 3 additions & 1 deletion server/plugins/example/templates/example_login.html
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,9 @@ <h2 id="authSuccessMessage"></h2>
"&password=" + encodeURIComponent(password);
console.log(nhpValidUrl);

fetch(nhpValidUrl)
fetch(nhpValidUrl,{
credentials: "include"
})
.then(response => response.json())
.then(result => {
console.log(result);
Expand Down

0 comments on commit 84db690

Please sign in to comment.