-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenMt.go
111 lines (92 loc) · 2.66 KB
/
tokenMt.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
package mutation
import (
"fmt"
"go-graphql-mongo-server/auth"
"go-graphql-mongo-server/common"
"go-graphql-mongo-server/gqlhandler/schema"
"go-graphql-mongo-server/logger"
"go-graphql-mongo-server/models"
"go-graphql-mongo-server/telemetry"
"strings"
"github.com/graphql-go/graphql"
"github.com/mitchellh/mapstructure"
"go.mongodb.org/mongo-driver/bson"
)
var CreateTokenMutation = &graphql.Field{
Name: "CreateToken",
Type: schema.TokenSchema,
Description: "Create a long lived personal access token",
Args: graphql.FieldConfigArgument{
"tokenName": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.String),
},
"expiresAt": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.DateTime),
},
"userName": &graphql.ArgumentConfig{
Type: graphql.String,
Description: "This username will only be used when Admin runs this mutation. Otherwise this will be ignored.",
},
},
Resolve: func(p graphql.ResolveParams) (i interface{}, e error) {
if common.IsValidUser(p) {
return nil, common.ErrUnauthorized
}
_, err := common.Sanitize(p.Args)
if err != nil {
return nil, err
}
defer telemetry.LogGraphQlCall(p, e)
userName := common.GetTokenUserName(p)
var token models.Token
//Decode input to token
err = mapstructure.Decode(p.Args, &token)
if err != nil {
logger.Log.Error(err)
}
token.UserName = userName
err = auth.GenerateToken(&token)
if err != nil {
return nil, err
}
err = models.Insert(p.Context, models.TokenCollection, token)
if err != nil && strings.Contains(err.Error(), "duplicate key error") {
return nil, fmt.Errorf("a token with same name already exists")
}
return token, err
},
}
var RevokeTokenMutation = &graphql.Field{
Name: "RevokeToken",
Type: graphql.Boolean,
Description: "Revoke a long lived personal access token",
Args: graphql.FieldConfigArgument{
"tokenName": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.String),
},
"userName": &graphql.ArgumentConfig{
Type: graphql.String,
Description: "This username will only be used when Admin runs this mutation. Otherwise this will be ignored.",
},
},
Resolve: func(p graphql.ResolveParams) (i interface{}, e error) {
if common.IsValidUser(p) {
return false, common.ErrUnauthorized
}
_, err := common.Sanitize(p.Args)
if err != nil {
return nil, err
}
defer telemetry.LogGraphQlCall(p, e)
userName := common.GetUserName(p)
err = models.Delete(
p.Context,
models.TokenCollection,
bson.M{"tokenName": p.Args["tokenName"].(string), "userName": userName},
)
if err != nil {
return false, err
}
return true, nil
},
}