Skip to content
Snippets Groups Projects
Unverified Commit ca1d9f1d authored by Angelo Capossele's avatar Angelo Capossele Committed by GitHub
Browse files

Fix: Web-auth plugin linter warnings (#360)

* :rotating_light: Fixes web auth linter warnings.

* :rotating_light: Fixes web auth imports order.
parent b33aa943
Branches
Tags
No related merge requests found
...@@ -5,13 +5,16 @@ import ( ...@@ -5,13 +5,16 @@ import (
) )
const ( const (
WEBAPI_AUTH_USERNAME = "webapi.auth.username" // CfgWebAPIAuthUsername defines the config flag of the web API authentication username.
WEBAPI_AUTH_PASSWORD = "webapi.auth.password" CfgWebAPIAuthUsername = "webapi.auth.username"
WEBAPI_AUTH_PRIVATE_KEY = "webapi.auth.privateKey" // 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() { func init() {
flag.String(WEBAPI_AUTH_USERNAME, "goshimmer", "username for the webapi") flag.String(CfgWebAPIAuthUsername, "goshimmer", "username for the webapi")
flag.String(WEBAPI_AUTH_PASSWORD, "goshimmer", "password for the webapi") flag.String(CfgWebAPIAuthPassword, "goshimmer", "password for the webapi")
flag.String(WEBAPI_AUTH_PRIVATE_KEY, "", "private key used to sign the JWTs") flag.String(CfgWebAPIAuthPrivateKey, "", "private key used to sign the JWTs")
} }
...@@ -5,15 +5,13 @@ import ( ...@@ -5,15 +5,13 @@ import (
"strings" "strings"
"time" "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/logger"
"github.com/iotaledger/hive.go/node" "github.com/iotaledger/hive.go/node"
"github.com/labstack/echo" "github.com/labstack/echo"
"github.com/labstack/echo/middleware" "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. // PluginName is the name of the web API auth plugin.
...@@ -28,7 +26,7 @@ var ( ...@@ -28,7 +26,7 @@ var (
func configure(plugin *node.Plugin) { func configure(plugin *node.Plugin) {
log = logger.NewLogger(PluginName) log = logger.NewLogger(PluginName)
privateKey = config.Node.GetString(WEBAPI_AUTH_PRIVATE_KEY) privateKey = config.Node.GetString(CfgWebAPIAuthPrivateKey)
if len(privateKey) == 0 { if len(privateKey) == 0 {
panic("") panic("")
} }
...@@ -47,23 +45,29 @@ func configure(plugin *node.Plugin) { ...@@ -47,23 +45,29 @@ func configure(plugin *node.Plugin) {
log.Info("WebAPI is now secured through JWT authentication") log.Info("WebAPI is now secured through JWT authentication")
} }
// Request defines the struct of the request.
type Request struct { type Request struct {
// Username is the username of the request.
Username string `json:"username"` Username string `json:"username"`
// Password is the password of the request.
Password string `json:"password"` Password string `json:"password"`
} }
// Response defines the struct of the response.
type Response struct { type Response struct {
// Token is the json web token.
Token string `json:"token"` Token string `json:"token"`
} }
// Handler handles the web auth request.
func Handler(c echo.Context) error { func Handler(c echo.Context) error {
login := &Request{} login := &Request{}
if err := c.Bind(login); err != nil { if err := c.Bind(login); err != nil {
return echo.ErrBadRequest return echo.ErrBadRequest
} }
if login.Username != config.Node.GetString(WEBAPI_AUTH_USERNAME) || if login.Username != config.Node.GetString(CfgWebAPIAuthUsername) ||
login.Password != config.Node.GetString(WEBAPI_AUTH_PASSWORD) { login.Password != config.Node.GetString(CfgWebAPIAuthPassword) {
return echo.ErrUnauthorized return echo.ErrUnauthorized
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment