forked from TheThingsNetwork/lorawan-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetadata.go
More file actions
135 lines (121 loc) · 4.17 KB
/
metadata.go
File metadata and controls
135 lines (121 loc) · 4.17 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package rpcmetadata contains utilities for transporting common request metadata over gRPC.
package rpcmetadata
import (
"context"
"strings"
"google.golang.org/grpc/metadata"
)
// MD contains The Things Stack metadata fields
type MD struct {
ID string
AuthType string
AuthValue string
ServiceType string
ServiceVersion string
NetAddress string
AllowInsecure bool
// Host is the hostname the request is directed to.
Host string
// URI is the URI the request is directed to.
URI string
// XForwardedFor is set from the X-Forwarded-For header.
XForwardedFor string
}
// RequireTransportSecurity returns true if authentication is configured
func (m MD) RequireTransportSecurity() bool {
if m.AuthType == "" || m.AuthValue == "" {
return false
}
return !m.AllowInsecure
}
// ToMetadata puts The Things Stack metadata fields in a metadata.MD
func (m MD) ToMetadata() metadata.MD {
var pairs []string
if m.ID != "" {
pairs = append(pairs, "id", m.ID)
}
if m.ServiceType != "" {
pairs = append(pairs, "service-type", m.ServiceType)
}
if m.ServiceVersion != "" {
pairs = append(pairs, "service-version", m.ServiceVersion)
}
if m.NetAddress != "" {
pairs = append(pairs, "net-address", m.NetAddress)
}
if m.Host != "" {
pairs = append(pairs, "host", m.Host)
}
if m.URI != "" {
pairs = append(pairs, "uri", m.URI)
}
if m.XForwardedFor != "" {
pairs = append(pairs, "x-forwarded-for", m.XForwardedFor)
}
return metadata.Pairs(pairs...)
}
// ToOutgoingContext puts The Things Stack metadata fields in an outgoing context.Context
func (m MD) ToOutgoingContext(ctx context.Context) context.Context {
md, _ := metadata.FromOutgoingContext(ctx)
md = metadata.Join(m.ToMetadata(), md)
return metadata.NewOutgoingContext(ctx, md)
}
// ToIncomingContext puts The Things Stack metadata fields in an incoming context.Context
func (m MD) ToIncomingContext(ctx context.Context) context.Context {
md, _ := metadata.FromIncomingContext(ctx)
md = metadata.Join(m.ToMetadata(), md)
return metadata.NewIncomingContext(ctx, md)
}
// FromMetadata returns The Things Stack metadata from metadata.MD
func FromMetadata(md metadata.MD) (m MD) {
if id, ok := md["id"]; ok && len(id) > 0 {
m.ID = id[len(id)-1]
}
if authorization, ok := md["authorization"]; ok && len(authorization) > 0 {
if parts := strings.SplitN(authorization[len(authorization)-1], " ", 2); len(parts) == 2 {
m.AuthType, m.AuthValue = parts[0], parts[1]
}
}
if serviceType, ok := md["service-type"]; ok && len(serviceType) > 0 {
m.ServiceType = serviceType[len(serviceType)-1]
}
if serviceVersion, ok := md["service-version"]; ok && len(serviceVersion) > 0 {
m.ServiceVersion = serviceVersion[len(serviceVersion)-1]
}
if netAddress, ok := md["net-address"]; ok && len(netAddress) > 0 {
m.NetAddress = netAddress[len(netAddress)-1]
}
if host, ok := md["host"]; ok && len(host) > 0 {
m.Host = host[len(host)-1]
}
if uri, ok := md["uri"]; ok && len(uri) > 0 {
m.URI = uri[len(uri)-1]
}
if xForwardedFor, ok := md["x-forwarded-for"]; ok && len(xForwardedFor) > 0 {
m.XForwardedFor = xForwardedFor[len(xForwardedFor)-1]
}
return
}
// FromOutgoingContext returns The Things Stack metadata from the outgoing context ctx.
func FromOutgoingContext(ctx context.Context) (m MD) {
md, _ := metadata.FromOutgoingContext(ctx)
return FromMetadata(md)
}
// FromIncomingContext returns The Things Stack metadata from the incoming context ctx.
func FromIncomingContext(ctx context.Context) (m MD) {
md, _ := metadata.FromIncomingContext(ctx)
return FromMetadata(md)
}