-
Notifications
You must be signed in to change notification settings - Fork 21
/
auth.go
174 lines (154 loc) · 4.58 KB
/
auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
(c) Copyright [2015] Hewlett Packard Enterprise Development LP
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package icsp
package icsp
import (
"encoding/json"
"strconv"
"strings"
"github.com/HewlettPackard/oneview-golang/rest"
"github.com/docker/machine/libmachine/log"
)
// URLEndPoint export this constant
const URLEndPointSession = "/rest/login-sessions"
// GetAuthHeaderMapNoVer Generate an auth Header map ...
// some api endpoints are hiddent, remove api version to get to them
func (c *ICSPClient) GetAuthHeaderMapNoVer() map[string]string {
return map[string]string{
"Content-Type": "application/json; charset=utf-8",
"auth": c.APIKey,
}
}
// GetAuthHeaderMap Generate an auth Header map ...
func (c *ICSPClient) GetAuthHeaderMap() map[string]string {
return map[string]string{
"Content-Type": "application/json; charset=utf-8",
"X-API-Version": strconv.Itoa(c.APIVersion),
"auth": c.APIKey,
}
}
// Session struct
type Session struct {
ID string `json:"sessionID,omitempty"`
}
// Auth structure
type Auth struct {
UserName string `json:"userName,omitempty"`
Password string `json:"password,omitempty"`
Domain string `json:"authLoginDomain,omitempty"`
}
// TimeOut structure
type TimeOut struct {
IdleTimeout int64 `json:"idleTimeout"`
}
// RefreshLogin Refresh login authkey
// Should make sure we have a valid APIKey
func (c *ICSPClient) RefreshLogin() error {
if c.APIKey == "" || len(strings.TrimSpace(c.APIKey)) == 0 || c.APIKey == "none" {
log.Debugf("Getting new session id")
s, err := c.SessionLogin()
if err != nil {
return err
}
c.APIKey = s.ID
}
// check it we are getting 404 Not Found from GetIdleTimeout, this means the Session-ID is no good
_, err := c.GetIdleTimeout()
if err != nil && strings.Contains(err.Error(), "404 Not Found") {
s, err := c.SessionLogin()
if err != nil {
return err
}
c.APIKey = s.ID
}
return nil
}
// SessionLogin to OneView and get a session ID
// returns Session structure
func (c *ICSPClient) SessionLogin() (Session, error) {
var (
uri = URLEndPointSession
body = Auth{UserName: c.User, Password: c.Password, Domain: c.Domain}
session Session
)
c.SetAuthHeaderOptions(c.GetAuthHeaderMap())
data, err := c.RestAPICall(rest.POST, uri, body)
if err != nil {
return session, err
}
log.Debugf("SessionLogin %s", data)
if err := json.Unmarshal([]byte(data), &session); err != nil {
return session, err
}
// Update APIKey
return session, err
}
// SessionLogout Logout to OneView and get a session ID
// returns Session structure
func (c *ICSPClient) SessionLogout() error {
var (
uri = "/rest/login-sessions"
)
log.Debugf("Calling logout for header -> %+v", c.GetAuthHeaderMap())
if c.APIKey == "none" {
log.Debugf("already logged out")
return nil
}
c.SetAuthHeaderOptions(c.GetAuthHeaderMap())
_, err := c.RestAPICall(rest.DELETE, uri, nil)
if err != nil {
return err
}
c.APIKey = "none"
return nil
}
// GetIdleTimeout gets the current timeout for the logged on session
// returns timeout in milliseconds, or error when it fails
func (c *ICSPClient) GetIdleTimeout() (int64, error) {
var (
uri = "/rest/sessions/idle-timeout"
timeout TimeOut
header map[string]string
)
log.Debugf("Calling idel-timeout get for header -> %+v", c.GetAuthHeaderMap())
header = c.GetAuthHeaderMap()
header["Session-ID"] = header["auth"]
c.SetAuthHeaderOptions(header)
data, err := c.RestAPICall(rest.GET, uri, nil)
if err != nil {
return -1, err
}
log.Debugf("Timeout data %s", data)
if err := json.Unmarshal([]byte(data), &timeout); err != nil {
return -1, err
}
return timeout.IdleTimeout, nil
}
// SetIdleTimeout sets the current timeout
func (c *ICSPClient) SetIdleTimeout(thetime int64) error {
var (
uri = "/rest/sessions/idle-timeout"
timeout TimeOut
header map[string]string
)
timeout.IdleTimeout = thetime
log.Debugf("Calling idel-timeout POST for header -> %+v", c.GetAuthHeaderMap())
header = c.GetAuthHeaderMap()
header["Session-ID"] = header["auth"]
c.SetAuthHeaderOptions(header)
_, err := c.RestAPICall(rest.POST, uri, timeout)
if err != nil {
return err
}
return nil
}