summaryrefslogtreecommitdiff
path: root/pkg/auth/sip2/client.go
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/auth/sip2/client.go')
-rw-r--r--pkg/auth/sip2/client.go69
1 files changed, 50 insertions, 19 deletions
diff --git a/pkg/auth/sip2/client.go b/pkg/auth/sip2/client.go
index 7116a84..9adf218 100644
--- a/pkg/auth/sip2/client.go
+++ b/pkg/auth/sip2/client.go
@@ -1,31 +1,50 @@
+// Copyright (C) 2019 LEAP
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
package sip2
import (
+ "0xacab.org/leap/vpnweb/pkg/auth/creds"
"fmt"
"github.com/reiver/go-telnet"
"log"
"time"
)
-const loginRequestTemplate string = "9300CN%s|CO%s|CP%s|"
-const statusRequestTemplate string = "23000%s %sAO%s|AA%s|AD%s|"
+const (
+ Label string = "sip2"
+ loginRequestTemplate string = "9300CN%s|CO%s|CP%s|"
+ statusRequestTemplate string = "23000%s %sAO%s|AA%s|AD%s|"
+)
-type Client struct {
- Host string
- Port string
+type sipClient struct {
+ host string
+ port string
location string
conn *telnet.Conn
parser *Parser
}
-func NewClient(host, port, location string) Client {
- c := Client{host, port, location, nil, nil}
+func newClient(host, port, location string) sipClient {
+ c := sipClient{host, port, location, nil, nil}
c.parser = getParser()
return c
}
-func (c *Client) Connect() (bool, error) {
- conn, err := telnet.DialTo(c.Host + ":" + c.Port)
+func (c *sipClient) Connect() (bool, error) {
+ conn, err := telnet.DialTo(c.host + ":" + c.port)
if nil != err {
log.Println("error", err)
return false, err
@@ -34,7 +53,7 @@ func (c *Client) Connect() (bool, error) {
return true, nil
}
-func (c *Client) Login(user, pass string) bool {
+func (c *sipClient) Login(user, pass string) bool {
loginStr := fmt.Sprintf(loginRequestTemplate, user, pass, c.location)
if nil == c.conn {
fmt.Println("error! null connection")
@@ -42,14 +61,31 @@ func (c *Client) Login(user, pass string) bool {
telnetSend(c.conn, loginStr)
loginResp := telnetRead(c.conn)
msg := c.parseResponse(loginResp)
- if value, ok := c.parser.getFixedFieldValue(msg, Ok); ok && value == TRUE {
+ if value, ok := c.parser.getFixedFieldValue(msg, okVal); ok && value == trueVal {
return true
}
return false
}
-func (c *Client) CheckCredentials(user, passwd string) bool {
+func (c *sipClient) parseResponse(txt string) *message {
+ msg := c.parser.parseMessage(txt)
+ return msg
+}
+
+/* Authenticator interface */
+
+func (c *sipClient) GetLabel() string {
+ return Label
+}
+
+func (c *sipClient) NeedsCredentials() bool {
+ return true
+}
+
+func (c *sipClient) CheckCredentials(credentials *creds.Credentials) bool {
currentTime := time.Now()
+ user := credentials.User
+ passwd := credentials.Password
statusRequest := fmt.Sprintf(
statusRequestTemplate,
currentTime.Format("20060102"),
@@ -58,8 +94,8 @@ func (c *Client) CheckCredentials(user, passwd string) bool {
telnetSend(c.conn, statusRequest)
statusMsg := c.parseResponse(telnetRead(c.conn))
- if value, ok := c.parser.getFieldValue(statusMsg, ValidPatron); ok && value == YES {
- if value, ok := c.parser.getFieldValue(statusMsg, ValidPatronPassword); ok && value == YES {
+ if value, ok := c.parser.getFieldValue(statusMsg, validPatron); ok && value == yes {
+ if value, ok := c.parser.getFieldValue(statusMsg, validPatronPassword); ok && value == yes {
return true
}
}
@@ -67,8 +103,3 @@ func (c *Client) CheckCredentials(user, passwd string) bool {
// TODO log whatever error we can find (AF, Screen Message, for instance)
return false
}
-
-func (c *Client) parseResponse(txt string) *Message {
- msg := c.parser.parseMessage(txt)
- return msg
-}