MCPcopy Create free account
hub / github.com/TheThingsNetwork/lorawan-stack / GenerateAPIKey

Function GenerateAPIKey

pkg/identityserver/api_key_utils.go:34–55  ·  view source on GitHub ↗

GenerateAPIKey generates a new API key with the given name for the set of rights

(ctx context.Context, name string, expiresAt *time.Time, rights ...ttnpb.Right)

Source from the content-addressed store, hash-verified

32
33// GenerateAPIKey generates a new API key with the given name for the set of rights
34func GenerateAPIKey(ctx context.Context, name string, expiresAt *time.Time, rights ...ttnpb.Right) (key *ttnpb.APIKey, token string, err error) {
35 token, err = auth.APIKey.Generate(ctx, "")
36 if err != nil {
37 return nil, "", err
38 }
39 _, generatedID, generatedKey, err := auth.SplitToken(token)
40 if err != nil {
41 panic(err) // Bug in either Generate or SplitToken.
42 }
43 hashedKey, err := auth.Hash(auth.NewContextWithHashValidator(ctx, apiKeyHashSettings), generatedKey)
44 if err != nil {
45 return nil, "", err
46 }
47 key = &ttnpb.APIKey{
48 Id: generatedID,
49 Key: hashedKey,
50 Name: name,
51 Rights: rights,
52 ExpiresAt: ttnpb.ProtoTime(expiresAt),
53 }
54 return key, token, nil
55}

Callers 4

createGatewayAPIKeyMethod · 0.85
createUserAPIKeyMethod · 0.85

Calls 5

SplitTokenFunction · 0.92
HashFunction · 0.92
ProtoTimeFunction · 0.92
GenerateMethod · 0.65

Tested by

no test coverage detected