Skip to content

Commit

Permalink
Added HTTP basic authentication (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
xBlaz3kx authored Jul 16, 2022
1 parent f6cda94 commit 08caba9
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 8 deletions.
2 changes: 2 additions & 0 deletions configs/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"protocolVersion": "1.6",
"serverUri": "example.com",
"maxChargingTime": 5,
"basicAuthUser": "",
"basicAuthPass": "",
"ocpp": {
"vendor": "UL FE",
"model": "ChargePi"
Expand Down
2 changes: 2 additions & 0 deletions docs/client/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ Example settings:
"id": "ChargePi",
"protocolVersion": "1.6",
"serverUri": "example.com",
"basicAuthUser": "",
"basicAuthPass": "",
"maxChargingTime": 5,
"ocpp": {
"vendor": "UL FE",
Expand Down
8 changes: 7 additions & 1 deletion internal/chargepoint/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package util

import (
"fmt"
"github.com/agrison/go-commons-lang/stringUtils"
ocpp16 "github.com/lorenzodonini/ocpp-go/ocpp1.6"
"github.com/lorenzodonini/ocpp-go/ocpp1.6/core"
"github.com/lorenzodonini/ocpp-go/ocpp1.6/firmware"
Expand Down Expand Up @@ -35,7 +36,7 @@ func CreateConnectionUrl(point settings.ChargePoint) string {
}

// CreateClient creates a Websocket client based on the settings.
func CreateClient(tlsConfig settings.TLS) *ws.Client {
func CreateClient(basicAuthUser, basicAuthPass string, tlsConfig settings.TLS) *ws.Client {
var (
client = ws.NewClient()
clientConfig = ws.NewClientTimeoutConfig()
Expand All @@ -54,6 +55,11 @@ func CreateClient(tlsConfig settings.TLS) *ws.Client {
client = tls.GetTLSClient(tlsConfig.CACertificatePath, tlsConfig.ClientCertificatePath, tlsConfig.ClientKeyPath)
}

// If HTTP basic auth is provided, set it in the Websocket client
if stringUtils.IsNoneEmpty(basicAuthUser, basicAuthPass) {
client.SetBasicAuth(basicAuthUser, basicAuthPass)
}

client.SetTimeoutConfig(clientConfig)
return client
}
Expand Down
7 changes: 5 additions & 2 deletions internal/chargepoint/v16/charge-point.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,11 @@ func (cp *ChargePoint) Init(settings *settings.Settings) {
var (
info = settings.ChargePoint.Info
tlsConfig = settings.ChargePoint.TLS
wsClient = chargePointUtil.CreateClient(tlsConfig)
logInfo = log.WithFields(log.Fields{
wsClient = chargePointUtil.CreateClient(
settings.ChargePoint.Info.BasicAuthUsername,
settings.ChargePoint.Info.BasicAuthPassword,
tlsConfig)
logInfo = log.WithFields(log.Fields{
"chargePointId": info.Id,
})
)
Expand Down
12 changes: 7 additions & 5 deletions internal/models/settings/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ type (
}

Info struct {
Id string `fig:"Id" validate:"required" json:"id,omitempty" yaml:"id" mapstructure:"id"`
ProtocolVersion string `fig:"ProtocolVersion" default:"1.6" json:"ProtocolVersion,omitempty" yaml:"ProtocolVersion" mapstructure:"ProtocolVersion"`
ServerUri string `fig:"ServerUri" validate:"required" json:"ServerUri,omitempty" yaml:"ServerUri" mapstructure:"ServerUri"`
MaxChargingTime int `fig:"MaxChargingTime" default:"180" json:"MaxChargingTime,omitempty" yaml:"MaxChargingTime" mapstructure:"MaxChargingTime"`
OCPPInfo OCPPInfo `fig:"ocpp" json:"ocpp" yaml:"ocpp" mapstructure:"ocpp"`
Id string `fig:"Id" validate:"required" json:"id,omitempty" yaml:"id" mapstructure:"id"`
ProtocolVersion string `fig:"ProtocolVersion" default:"1.6" json:"ProtocolVersion,omitempty" yaml:"ProtocolVersion" mapstructure:"ProtocolVersion"`
ServerUri string `fig:"ServerUri" validate:"required" json:"ServerUri,omitempty" yaml:"ServerUri" mapstructure:"ServerUri"`
BasicAuthUsername string `fig:"basicAuthUser" json:"basicAuthUser,omitempty" yaml:"basicAuthUser" mapstructure:"basicAuthUser"`
BasicAuthPassword string `fig:"basicAuthPass" json:"basicAuthPass,omitempty" yaml:"basicAuthPass" mapstructure:"basicAuthPass"`
MaxChargingTime int `fig:"MaxChargingTime" default:"180" json:"MaxChargingTime,omitempty" yaml:"MaxChargingTime" mapstructure:"MaxChargingTime"`
OCPPInfo OCPPInfo `fig:"ocpp" json:"ocpp" yaml:"ocpp" mapstructure:"ocpp"`
}

TLS struct {
Expand Down

0 comments on commit 08caba9

Please sign in to comment.