-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.go
102 lines (92 loc) · 2.57 KB
/
logger.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
package keycloak
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"sync"
)
type logger struct {
filename string
*log.Logger
}
var userLog *logger
var appLog *logger
var logs *logger
var once sync.Once
var file *os.File
var fileStat os.FileInfo
var files map[string]os.FileInfo
//GetInstance returns a new logger to a file
func getInstance(name string) *logger {
logs = createLogger("./log/" + name)
return logs
}
func createLogger(fname string) *logger {
if files == nil {
files = make(map[string]os.FileInfo, 2)
}
file, _ = os.OpenFile(fname, os.O_APPEND|os.O_RDWR|os.O_CREATE, 0644)
fileStat, _ = file.Stat()
files[fname] = fileStat
return &logger{
filename: fname,
Logger: log.New(file, "", log.Ldate|log.Ltime),
}
}
func logAction(username string, a Action, additional string) {
if files["./log/UserLogs.log"] == nil {
userLog = getInstance("UserLogs.log")
}
userLog.Println(username+": ", a, " ", additional)
}
//LogAction is an external call for logging actions into the file log
func LogAction(a Action, additional, tokenString string) {
username := GetUsername(tokenString)
email := GetEmail(tokenString)
fmt.Printf("got username and gmail")
if files["./log/AppLogs.log"] == nil {
fmt.Printf("Found file not made")
appLog = getInstance("AppLogs.log")
fmt.Printf("Made file")
}
appLog.Println(username+";"+email+": ", a, " ", additional)
}
//GetUsername gets the current users username
func GetUsername(tokenString string) string {
client := &http.Client{}
url := keycloakserver + "/auth/realms/" + realm + "/protocol/openid-connect/userinfo"
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("Authorization", "Bearer "+tokenString)
//Check if token is still valid
response, err := client.Do(req)
if err != nil || response.Status != "200 OK" {
return ""
}
body, _ := ioutil.ReadAll(response.Body)
var f interface{}
json.Unmarshal(body, &f)
m := f.(map[string]interface{})
username := m["preferred_username"].(string)
return username
}
//GetEmail gets the current users username
func GetEmail(tokenString string) string {
client := &http.Client{}
url := keycloakserver + "/auth/realms/" + realm + "/protocol/openid-connect/userinfo"
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("Authorization", "Bearer "+tokenString)
//Check if token is still valid
response, err := client.Do(req)
if err != nil || response.Status != "200 OK" {
return ""
}
body, _ := ioutil.ReadAll(response.Body)
var f interface{}
json.Unmarshal(body, &f)
m := f.(map[string]interface{})
username := m["email"].(string)
return username
}