/
mytoken.go
44 lines (39 loc) · 1.44 KB
/
mytoken.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
package ssh
import (
"encoding/json"
"github.com/gliderlabs/ssh"
"github.com/oidc-mytoken/api/v0"
"github.com/oidc-mytoken/server/internal/endpoints/token/mytoken/pkg"
"github.com/oidc-mytoken/server/internal/utils/logger"
"github.com/oidc-mytoken/server/shared/model"
mytoken2 "github.com/oidc-mytoken/server/shared/mytoken"
mytoken "github.com/oidc-mytoken/server/shared/mytoken/pkg"
"github.com/oidc-mytoken/server/shared/utils/ternary"
)
func handleSSHMytoken(reqData []byte, s ssh.Session) error {
ctx := s.Context()
req := pkg.NewMytokenRequest()
req.GrantType = model.GrantTypeMytoken
if len(reqData) > 0 {
if err := json.Unmarshal(reqData, &req); err != nil {
return err
}
}
clientMetaData := api.ClientMetaData{
IP: ctx.Value("ip").(string),
UserAgent: ctx.Value("user_agent").(string),
}
req.Mytoken = ctx.Value("mytoken").(*mytoken.Mytoken).ToUniversalMytoken()
rlog := logger.GetSSHRequestLogger(ctx.Value("session").(string))
rlog.Debug("Handle mytoken from ssh")
usedRestriction, mt, errRes := mytoken2.HandleMytokenFromMytokenReqChecks(rlog, req, clientMetaData.IP, nil)
if errRes != nil {
return writeErrRes(s, errRes)
}
res := mytoken2.HandleMytokenFromMytokenReq(rlog, mt, req, &clientMetaData, usedRestriction)
if res.Status >= 400 {
return writeErrRes(s, res)
}
tokenRes := res.Response.(pkg.MytokenResponse)
return writeString(s, ternary.IfNotEmptyOr(tokenRes.Mytoken, tokenRes.TransferCode))
}