-
Notifications
You must be signed in to change notification settings - Fork 26
/
dao_es7.go
101 lines (88 loc) · 2.38 KB
/
dao_es7.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
package auth
import (
"context"
"encoding/json"
"errors"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/appbaseio/reactivesearch-api/model/credential"
"github.com/appbaseio/reactivesearch-api/model/permission"
"github.com/appbaseio/reactivesearch-api/model/user"
"github.com/appbaseio/reactivesearch-api/util"
es7 "github.com/olivere/elastic/v7"
)
func (es *elasticsearch) getPublicKeyEs7(ctx context.Context, publicKeyIndex, publicKeyDocID string) (publicKey, error) {
var record = publicKey{}
response, err := util.GetClient7().Get().
Index(publicKeyIndex).
Id(publicKeyDocID).
Do(ctx)
if response == nil {
return record, errors.New("public key record not found")
}
err = json.Unmarshal(response.Source, &record)
if err != nil {
log.Errorln(logTag, ": error retrieving publickey record", err)
return record, err
}
return record, nil
}
func (es *elasticsearch) getCredentialEs7(ctx context.Context, username string) (credential.AuthCredential, error) {
matchUsername := es7.NewTermQuery("username.keyword", username)
response, err := util.GetClient7().Search().
Index(es.userIndex, es.permissionIndex).
Query(matchUsername).
FetchSource(true).
Do(ctx)
if err != nil {
return nil, err
}
if len(response.Hits.Hits) > 1 {
return nil, fmt.Errorf(`more than one result for "username"="%s"`, username)
}
// there should be either 0 or 1 hit
var obj credential.AuthCredential
for _, hit := range response.Hits.Hits {
if hit.Index == es.userIndex {
var u user.User
if hit.Source != nil {
err := json.Unmarshal(hit.Source, &u)
if err != nil {
return nil, err
}
obj = &u
}
} else if hit.Index == es.permissionIndex {
var p permission.Permission
// unmarshal into permission
err := json.Unmarshal(hit.Source, &p)
if err != nil {
return nil, err
}
obj = &p
}
}
if obj == nil {
return nil, fmt.Errorf(`invalid username or password`)
}
return obj, nil
}
func (es *elasticsearch) getRawRolePermissionEs7(ctx context.Context, role string) ([]byte, error) {
resp, err := util.GetClient7().Search().
Index(es.permissionIndex).
Type(es.permissionType).
Query(es7.NewTermQuery("role.keyword", role)).
Size(1).
FetchSource(true).
Do(ctx)
if err != nil {
return nil, err
}
for _, hit := range resp.Hits.Hits {
src, err := json.Marshal(hit.Source)
if err == nil {
return src, nil
}
}
return nil, nil
}