This repository has been archived by the owner on Feb 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathssh.go
99 lines (83 loc) · 2.81 KB
/
ssh.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
/*
Copyright 2019 Padduck, LLC
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 oauth2
import (
"encoding/json"
"errors"
"github.com/pufferpanel/apufferi/v4/logging"
"github.com/pufferpanel/pufferd/v2/commons"
"golang.org/x/crypto/ssh"
"io/ioutil"
"net/url"
"strconv"
"strings"
)
type WebSSHAuthorization struct {
}
func (ws *WebSSHAuthorization) Validate(username string, password string) (*ssh.Permissions, error) {
return validateSSH(username, password, true)
}
func validateSSH(username string, password string, recurse bool) (*ssh.Permissions, error) {
data := url.Values{}
data.Set("grant_type", "password")
data.Set("username", username)
data.Set("password", password)
data.Set("scope", "sftp")
encodedData := data.Encode()
request := createRequest(encodedData)
RefreshIfStale()
atLocker.RLock()
request.Header.Add("Authorization", "Bearer "+daemonToken)
atLocker.RUnlock()
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
request.Header.Add("Content-Length", strconv.Itoa(len(encodedData)))
response, err := client.Do(request)
defer commons.CloseResponse(response)
if err != nil {
logging.Exception("error talking to auth server", err)
return nil, errors.New("invalid response from authorization server")
}
//we should only get a 200, if we get any others, we have a problem
if response.StatusCode != 200 {
if response.StatusCode == 401 {
if recurse && RefreshToken() {
commons.CloseResponse(response)
return validateSSH(username, password, false)
}
}
msg, _ := ioutil.ReadAll(response.Body)
logging.Error("Error talking to auth server: [%d] [%s]", response.StatusCode, msg)
return nil, errors.New("invalid response from authorization server")
}
var respArr map[string]interface{}
err = json.NewDecoder(response.Body).Decode(&respArr)
if err != nil {
return nil, err
}
if respArr["error"] != nil {
return nil, errors.New("incorrect username or password")
}
sshPerms := &ssh.Permissions{}
scopes := strings.Split(respArr["scope"].(string), " ")
if len(scopes) != 2 {
return nil, errors.New("invalid response from authorization server")
}
for _, v := range scopes {
if v != "sftp" {
sshPerms.Extensions = make(map[string]string)
sshPerms.Extensions["server_id"] = v
return sshPerms, nil
}
}
return nil, errors.New("incorrect username or password")
}