-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethods.go
More file actions
104 lines (86 loc) · 2.13 KB
/
Copy pathmethods.go
File metadata and controls
104 lines (86 loc) · 2.13 KB
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
// 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
import (
"context"
"log"
"runtime"
"sync"
)
// accesso is an interface to manage credentials.
type accesso interface {
// autenticato method returns true whether credentials
// are found in any storage (json file or sql db).
Autenticato(context.Context) bool
// token method returns a pseudo token if credentials are good.
GetToken(context.Context) string
}
// Autenticato returns true if credentials are found in any storage.
func (c *Credentials) Autenticato(ctx context.Context) bool {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
// Istanzia un wait group per gestire i processi paralleli.
var wg sync.WaitGroup
var globallyAuthenticated = make(chan bool, 1)
// Aggiunge un processo parallelo.
wg.Add(1)
go func() {
defer runtime.Gosched()
defer wg.Done()
isAuthenticated, err := CheckCredentialsDBCtx(ctx, c)
if err != nil {
log.Printf("Error: %v", err)
}
defer log.Printf("Finito controllo su DB:\t%v,\tid:\t%s\n",
isAuthenticated,
ctx.Value(k))
if isAuthenticated {
globallyAuthenticated <- true
}
return
}()
// Aggiunge un processo parallelo.
wg.Add(1)
go func() {
defer runtime.Gosched()
defer wg.Done()
isAuthenticated, err := CheckLocalCredentials(ctx, c)
if err != nil {
log.Printf("Error: %v", err)
}
defer log.Printf("Finito controllo su File:\t%v,\tid:\t%s\n",
isAuthenticated,
ctx.Value(k))
if isAuthenticated {
globallyAuthenticated <- true
}
return
}()
go func() {
// Aspetta che tutti i processi paralleli terminino.
wg.Wait()
globallyAuthenticated <- false
return
}()
select {
case <-ctx.Done():
log.Printf("Error in autenticato %v function: %v\n",
ctx.Value(k),
ctx.Err())
return false
case result := <-globallyAuthenticated:
return result
}
}
// GetToken ...
func (c Credentials) GetToken(ctx context.Context) string {
if c.Autenticato(ctx) {
token, err := GenerateCtx(ctx)
if err != nil {
log.Println(err)
}
return token
}
return ""
}