Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
id
keystore
Commits
e7fb4821
Commit
e7fb4821
authored
Dec 09, 2017
by
ale
Browse files
Add a client package
parent
8c19ba4e
Changes
1
Hide whitespace changes
Inline
Side-by-side
client/client.go
0 → 100644
View file @
e7fb4821
package
client
import
(
"context"
"crypto/tls"
"net/http"
"net/url"
"time"
"git.autistici.org/ai3/go-common/clientutil"
"git.autistici.org/id/keystore"
)
// Client for the keystore API.
type
Client
struct
{
*
http
.
Client
backendURL
string
}
// Config for a keystore client.
type
Config
struct
{
BackendURL
string
`yaml:"backend_url"`
TLSConfig
*
clientutil
.
TLSClientConfig
`yaml:"tls_config"`
}
// New returns a new Client with the given Config.
func
New
(
config
*
Config
)
(
*
Client
,
error
)
{
u
,
err
:=
url
.
Parse
(
config
.
BackendURL
)
if
err
!=
nil
{
return
nil
,
err
}
var
tlsConfig
*
tls
.
Config
if
config
.
TLSConfig
!=
nil
{
tlsConfig
,
err
=
config
.
TLSConfig
.
TLSConfig
()
if
err
!=
nil
{
return
nil
,
err
}
}
c
:=
&
http
.
Client
{
Transport
:
clientutil
.
NewTransport
([]
string
{
u
.
Host
},
tlsConfig
,
nil
),
Timeout
:
20
*
time
.
Second
,
}
return
&
Client
{
Client
:
c
,
backendURL
:
config
.
BackendURL
,
},
nil
}
func
(
c
*
Client
)
Open
(
ctx
context
.
Context
,
username
,
password
string
,
ttl
int
)
error
{
req
:=
keystore
.
OpenRequest
{
Username
:
username
,
Password
:
password
,
TTL
:
ttl
,
}
var
resp
keystore
.
OpenResponse
return
clientutil
.
DoJSONHTTPRequest
(
ctx
,
c
.
Client
,
c
.
backendURL
+
"/api/open"
,
&
req
,
&
resp
)
}
func
(
c
*
Client
)
Get
(
ctx
context
.
Context
,
username
,
ssoTicket
string
)
([]
byte
,
error
)
{
req
:=
keystore
.
GetRequest
{
Username
:
username
,
SSOTicket
:
ssoTicket
,
}
var
resp
keystore
.
GetResponse
err
:=
clientutil
.
DoJSONHTTPRequest
(
ctx
,
c
.
Client
,
c
.
backendURL
+
"/api/get"
,
&
req
,
&
resp
)
return
resp
.
Key
,
err
}
func
(
c
*
Client
)
Close
(
ctx
context
.
Context
,
username
string
)
error
{
req
:=
keystore
.
CloseRequest
{
Username
:
username
,
}
var
resp
keystore
.
CloseResponse
return
clientutil
.
DoJSONHTTPRequest
(
ctx
,
c
.
Client
,
c
.
backendURL
+
"/api/close"
,
&
req
,
&
resp
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment