-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverter.go
82 lines (74 loc) · 2.2 KB
/
converter.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
package andotpconverter
import (
"time"
"github.com/dereulenspiegel/andotp-converter/andotp"
"github.com/dereulenspiegel/andotp-converter/twofas"
"github.com/google/uuid"
)
func FromAndOtpTo2Fas(andOTPServices []*andotp.Service) (*twofas.RemoteBackup, error) {
updatedAt := uint64(time.Now().UnixMilli())
twofasBackup := &twofas.RemoteBackup{
UpdatedAt: updatedAt,
SchemaVersion: twofas.DEFAULT_SCHEMA_VERSION,
AppVersionCode: twofas.DEFAULT_APP_VERSION_CODE,
AppOrigin: twofas.DEAFULT_APP_ORIGIN,
}
groupMapping := map[string]twofas.RemoteGroup{}
defaultSource := twofas.DEFAULT_SOURCE
for i, andOTPService := range andOTPServices {
var groupId *string
if len(andOTPService.Tags) > 0 {
// Use the first tag as group
tag := andOTPService.Tags[0]
if group, exists := groupMapping[tag]; !exists {
group = twofas.RemoteGroup{
Name: tag,
UpdatedAt: updatedAt,
IsExpanded: true,
ID: uuid.Must(uuid.NewRandom()).String(),
}
groupMapping[tag] = group
groupId = &group.ID
} else {
groupId = &group.ID
}
}
name := andOTPService.Issuer
if name == "" {
name = andOTPService.Label
}
remoteService := twofas.RemoteService{
Name: name,
Secret: andOTPService.Secret,
UpdatedAt: andOTPService.LastUsed,
Order: twofas.Order{Position: i},
Type: thumbnailToServiceType(andOTPService.Thumbnail),
GroupId: groupId,
Otp: twofas.Otp{
Label: &andOTPService.Label,
Account: &andOTPService.Label,
Issuer: &andOTPService.Issuer,
Digits: &andOTPService.Digits,
Period: &andOTPService.Period,
Algorithm: &andOTPService.Algorithm,
Counter: &andOTPService.UsedFrequency,
TokenType: &andOTPService.Type,
Source: &defaultSource,
},
}
twofasBackup.Services = append(twofasBackup.Services, &remoteService)
}
for _, group := range groupMapping {
twofasBackup.Groups = append(twofasBackup.Groups, &group)
}
return twofasBackup, nil
}
func thumbnailToServiceType(thumbnail string) *twofas.ServiceType {
for _, st := range twofas.ServiceTypes {
if string(st) == thumbnail {
return &st
}
}
unknown := twofas.ServiceType("Unknwown")
return &unknown
}