-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgettoken.go
118 lines (92 loc) · 2.56 KB
/
gettoken.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
// Copyright 2019 Alberto Bregliano. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package token generates pseudo uddi tokens if credentials used
// match any storage.
package token
import (
"context"
crand "crypto/rand"
"encoding/binary"
"encoding/json"
"fmt"
"io/ioutil"
"log"
rand "math/rand"
"github.com/axamon/bloomfilter"
)
// CredentialsJSONFile is the json file containing credentials.
var CredentialsJSONFile = "credentialsdb.json"
var src cryptoSource
var f *bloomfilter.BloomFilter
func init() {
var err error
rnd := rand.New(src)
rnd.Seed(rnd.Int63())
f = bloomfilter.New()
body, err := ioutil.ReadFile(CredentialsJSONFile)
var db = new(credentialsDB)
err = json.Unmarshal(body, &db)
if err != nil {
log.Printf(
"Error in unmarshalling %s: %v", CredentialsJSONFile, err)
}
for _, r := range db.UserpassDB {
f.Add(r.UsernameDB)
}
}
// GenerateCtx generates a token.
func GenerateCtx(ctx context.Context) (string, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
select {
case <-ctx.Done():
return "", fmt.Errorf("function GenerateCtx in error: %v", ctx.Err())
default:
b := make([]byte, 16)
_, err := rand.Read(b)
uuid := fmt.Sprintf("%x-%x-%x-%x-%x",
b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
return uuid, err
}
}
// CheckLocalCredentials verifies username and passwords on local json file.
func CheckLocalCredentials(ctx context.Context, c *Credentials) (bool, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
// Verify on bloomfilter created in init the presence of username.
// If not present exits.
if !f.Exists(c.User) {
return false, fmt.Errorf("User %s not found in bloomfilter", c.User)
}
body, err := ioutil.ReadFile(CredentialsJSONFile)
var db = new(credentialsDB)
err = json.Unmarshal(body, &db)
if err != nil {
return false, fmt.Errorf(
"Error in unmarshalling %s: %v", CredentialsJSONFile, err)
}
for _, r := range db.UserpassDB {
if r.UsernameDB == c.User && r.PasswordDB == c.Hashpass {
return true, nil
}
}
select {
case <-ctx.Done():
return false, fmt.Errorf(
"function checkCredentials in error: %v", ctx.Err())
default:
return false, nil
}
}
func (s cryptoSource) Seed(seed int64) {}
func (s cryptoSource) Int63() int64 {
return int64(s.Uint64() & ^uint64(1<<63))
}
func (s cryptoSource) Uint64() (v uint64) {
err := binary.Read(crand.Reader, binary.BigEndian, &v)
if err != nil {
log.Fatalf("Low entropy, cannot create crypto random number: %v", err)
}
return v
}