diff --git a/plugins/webauth/parameters.go b/plugins/webauth/parameters.go index 5084ce564029b3dc5e7d4ffc2ed8380e8fd1e3c9..e9c2eb20ff7e26c359d034346d72aea15ee61b3b 100644 --- a/plugins/webauth/parameters.go +++ b/plugins/webauth/parameters.go @@ -5,13 +5,16 @@ import ( ) const ( - WEBAPI_AUTH_USERNAME = "webapi.auth.username" - WEBAPI_AUTH_PASSWORD = "webapi.auth.password" - WEBAPI_AUTH_PRIVATE_KEY = "webapi.auth.privateKey" + // CfgWebAPIAuthUsername defines the config flag of the web API authentication username. + CfgWebAPIAuthUsername = "webapi.auth.username" + // CfgWebAPIAuthPassword defines the config flag of the web API authentication password. + CfgWebAPIAuthPassword = "webapi.auth.password" + // CfgWebAPIAuthPrivateKey defines the config flag of the web API authentication private key. + CfgWebAPIAuthPrivateKey = "webapi.auth.privateKey" ) func init() { - flag.String(WEBAPI_AUTH_USERNAME, "goshimmer", "username for the webapi") - flag.String(WEBAPI_AUTH_PASSWORD, "goshimmer", "password for the webapi") - flag.String(WEBAPI_AUTH_PRIVATE_KEY, "", "private key used to sign the JWTs") + flag.String(CfgWebAPIAuthUsername, "goshimmer", "username for the webapi") + flag.String(CfgWebAPIAuthPassword, "goshimmer", "password for the webapi") + flag.String(CfgWebAPIAuthPrivateKey, "", "private key used to sign the JWTs") } diff --git a/plugins/webauth/webauth.go b/plugins/webauth/webauth.go index f4f45b55e7493a45d566e6b7d323d55961ddc651..946f0c8b29bca8fab1ea379d8ab95bcef8fa21a2 100644 --- a/plugins/webauth/webauth.go +++ b/plugins/webauth/webauth.go @@ -5,15 +5,13 @@ import ( "strings" "time" + "github.com/dgrijalva/jwt-go" + "github.com/iotaledger/goshimmer/plugins/config" + "github.com/iotaledger/goshimmer/plugins/webapi" "github.com/iotaledger/hive.go/logger" "github.com/iotaledger/hive.go/node" "github.com/labstack/echo" "github.com/labstack/echo/middleware" - - "github.com/iotaledger/goshimmer/plugins/config" - "github.com/iotaledger/goshimmer/plugins/webapi" - - "github.com/dgrijalva/jwt-go" ) // PluginName is the name of the web API auth plugin. @@ -28,7 +26,7 @@ var ( func configure(plugin *node.Plugin) { log = logger.NewLogger(PluginName) - privateKey = config.Node.GetString(WEBAPI_AUTH_PRIVATE_KEY) + privateKey = config.Node.GetString(CfgWebAPIAuthPrivateKey) if len(privateKey) == 0 { panic("") } @@ -47,23 +45,29 @@ func configure(plugin *node.Plugin) { log.Info("WebAPI is now secured through JWT authentication") } +// Request defines the struct of the request. type Request struct { + // Username is the username of the request. Username string `json:"username"` + // Password is the password of the request. Password string `json:"password"` } +// Response defines the struct of the response. type Response struct { + // Token is the json web token. Token string `json:"token"` } +// Handler handles the web auth request. func Handler(c echo.Context) error { login := &Request{} if err := c.Bind(login); err != nil { return echo.ErrBadRequest } - if login.Username != config.Node.GetString(WEBAPI_AUTH_USERNAME) || - login.Password != config.Node.GetString(WEBAPI_AUTH_PASSWORD) { + if login.Username != config.Node.GetString(CfgWebAPIAuthUsername) || + login.Password != config.Node.GetString(CfgWebAPIAuthPassword) { return echo.ErrUnauthorized }