/
identity.go
170 lines (159 loc) · 4.4 KB
/
identity.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
// Copyright 2018 The go-fireblock Authors
// This file is part of the go-fireblock library.
//
// The go-fireblock library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-fireblock library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-fireblock library. If not, see <http://www.gnu.org/licenses/>.
//
// Package common contains various helper functions.
package fireblocklib
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"regexp"
)
func extractFrom(txt string) (string, string, string, string) {
// I,ellis2323 am cjFDSTdYZ3pN on Fireblock and use the 0x99090eae43316b2ba65ec52bcd5834a3e07edb2c pgp key (https://fireblock.io)
reg, err := regexp.Compile(`I,([\w\-]+) am ([\w]+) on Fireblock and use the (0x\w+) (ecdsa|pgp|eth) key`)
if err != nil {
return "", "", "", ""
}
k := reg.FindStringSubmatch(txt)
if len(k) != 5 {
return "", "", "", ""
}
return k[2], k[1], k[4], k[3]
}
func extractFromHTTPS(txt string) (string, string, string) {
// I am rJpoNe2zm on Fireblock and use the 0x99090eae43316b2ba65ec52bcd5834a3e07edb2c pgp key (https://fireblock.io )
reg, err := regexp.Compile(`I am ([\w]+) on Fireblock and use the (0x\w+) (ecdsa|pgp|eth) key`)
if err != nil {
return "", "", ""
}
k := reg.FindStringSubmatch(txt)
if len(k) != 4 {
return "", "", ""
}
return k[1], k[3], k[2]
}
func getURLContent(url string) (string, error) {
res, err := http.Get(url)
if err != nil {
msg := fmt.Sprintf("invalid url: %s", url)
return "", NewFBKError(msg, NetworkError)
}
buf := new(bytes.Buffer)
buf.ReadFrom(res.Body)
s := buf.String()
return s, nil
}
// CheckTwitter check twitter proof
func CheckTwitter(url, twUID, useruid, fingerprint string) bool {
reg, err := regexp.Compile(`https://twitter.com/(\w{1,15})/status/`)
if err != nil {
return false
}
if !reg.MatchString(url) {
return false
}
k := reg.FindStringSubmatch(url)
if k[1] != twUID {
return false
}
proof, err2 := getURLContent(url)
if err2 != nil {
return false
}
luid, twuid, _, fp := extractFrom(proof)
if luid != useruid || twuid != twUID || fp != fingerprint {
return false
}
return true
}
// OwnerData login
type OwnerData struct {
Login string `json:"login"`
}
// GistData Gist information
type GistData struct {
Owner OwnerData `json:"owner"`
ForkOf json.RawMessage `json:"fork_of"`
}
// CheckGithub Check github proof
func CheckGithub(url, ghUID, useruid, fingerprint string) bool {
reg, err := regexp.Compile(`^https://gist.github.com/(\w{1,15})/(\w+)`)
if err != nil {
return false
}
k := reg.FindStringSubmatch(url)
if k[1] != ghUID {
return false
}
// content
newURL := `https://gist.githubusercontent.com/` + k[1] + `/` + k[2] + `/raw`
proof, err4 := getURLContent(newURL)
if err4 != nil {
return false
}
luid, ghuid, _, fp := extractFrom(proof)
if luid != useruid || ghuid != ghUID || fp != fingerprint {
return false
}
return true
}
// CheckLinkedin check a linkedin proof
func CheckLinkedin(url, lkUID, useruid, fingerprint string) bool {
proof, err := getURLContent(url)
if err != nil {
return false
}
reg, err2 := regexp.Compile(`https://\w+.linkedin.com/in/` + lkUID)
if err2 != nil {
return false
}
res := reg.FindAllString(proof, -1)
if len(res) == 0 {
return false
}
// check
luid, lkuid, _, fp := extractFrom(proof)
if luid != useruid || lkuid != lkUID || fp != fingerprint {
return false
}
return true
}
// CheckHTTPS check website with a https proof
func CheckHTTPS(url, dnsUID, useruid, fingerprint string) bool {
url2 := `^https://` + dnsUID + `/.well-known/` + fingerprint + `.txt$`
reg, err2 := regexp.Compile(url2)
if err2 != nil {
return false
}
resR := reg.MatchString(url)
if !resR {
return false
}
proof, err := getURLContent(url)
if err != nil {
return false
}
luid, ktype, fp := extractFromHTTPS(proof)
if ktype != "eth" && ktype != "ecdsa" && ktype != "pgp" {
return false
}
if luid != useruid || fp != fingerprint {
return false
}
return true
}