This repository has been archived by the owner on May 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
endpoint.go
147 lines (132 loc) · 5.65 KB
/
endpoint.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
MIT License
Copyright (c) 2017 MichiVIP
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package skypeapi
import (
"net/http"
"crypto/tls"
"encoding/json"
)
const (
defaultPath string = "/"
defaultTlsHeaderValue string = "max-age=63072000; includeSubDomains" // max-age in seconds which matches 2 years
authorizationHeaderKey string = "Authorization"
)
type Endpoint struct {
// Explanation: The address the server should listen on. This declares the port and the ip.
// Example: ":2345" The application would run on 0.0.0.0 with the port 2345
Address string
// Explanation: The path which the server receives its requests.
// Example: If the value is set to "/skype/" than the server would listen on "https://domain.tld/skype/"
Path string
// Explanation: The TLSConfig which declares which values will be sent to a client
TLSConfig *tls.Config
}
// Returns a new Endpoint struct object with the default request path "/".
func NewEndpoint(address string) (*Endpoint) {
// the default TLS config
cfg := &tls.Config{
MinVersion: tls.VersionTLS12,
CurvePreferences: []tls.CurveID{tls.CurveP521, tls.CurveP384, tls.CurveP256},
PreferServerCipherSuites: true,
CipherSuites: []uint16{
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_RSA_WITH_AES_256_CBC_SHA,
},
}
return &Endpoint{
Address: address,
Path: defaultPath,
TLSConfig: cfg,
}
}
type EndpointHandler struct {
// The MicrosoftAppId is used to authorize incoming requests
MicrosoftAppId string
// The authorization token which is used to authorize incoming requests
AuthorizationToken string
// The header value which will be sent to the client with the "Strict-Transport-Security" key
TlsHeaderValue string
// The function to handle incoming decoded Activity object
ActivityReceivedHandleFunction func(activity *Activity)
}
// The activityReceivedHandleFunction will gets called on incoming Activity objects for example incoming skype messages.
// The authorization token which is used to authenticate incoming requests by the microsoft servers.
// The microsoftAppId which is used to authorize incoming requests
// Returns a new Endpoint struct object with the default Strict-Transport-Security Header "max-age=63072000; includeSubDomains".
func NewEndpointHandler(activityReceivedHandleFunction func(activity *Activity), authorizationToken, microsoftAppId string) (*EndpointHandler) {
endpointHandler := &EndpointHandler{
AuthorizationToken: authorizationToken,
TlsHeaderValue: defaultTlsHeaderValue,
ActivityReceivedHandleFunction: activityReceivedHandleFunction,
MicrosoftAppId: microsoftAppId,
}
return endpointHandler
}
// This method does not cache the SigningKeys
// The req which should be proved
func (endpointHandler EndpointHandler) IsAuthorized(req *http.Request) bool {
signingKeys, err := GetSigningKeys()
if err != nil {
return false
} else {
return endpointHandler.IsAuthorizedWithSigningKeys(req, signingKeys)
}
}
// The req which should be proved
// The SigningKeys which can be used to authorize the request
func (endpointHandler EndpointHandler) IsAuthorizedWithSigningKeys(req *http.Request, signingKeys SigningKeys) bool {
var authorizationValue string = req.Header.Get(authorizationHeaderKey)
if microsoftJsonWebToken, err := ParseMicrosoftJsonWebToken(authorizationValue);
err != nil {
return false
} else {
return microsoftJsonWebToken.Verify(endpointHandler.MicrosoftAppId, signingKeys)
}
}
// Internal method to hook skype actions.
func (endpointHandler EndpointHandler) ServeHTTP(responseWriter http.ResponseWriter, req *http.Request) {
if len(endpointHandler.TlsHeaderValue) != 0 {
responseWriter.Header().Add("Strict-Transport-Security", endpointHandler.TlsHeaderValue)
}
var activity Activity
if !endpointHandler.IsAuthorized(req) {
responseWriter.WriteHeader(http.StatusForbidden)
} else if err := json.NewDecoder(req.Body).Decode(&activity); err == nil {
responseWriter.WriteHeader(http.StatusOK)
endpointHandler.ActivityReceivedHandleFunction(&activity)
} else {
responseWriter.WriteHeader(http.StatusBadRequest)
}
}
// This method could be used on an Endpoint struct object to setup an own web server which
// handles skype actions. The returned http.Server can still be edited to
func (endpoint Endpoint) SetupServer(handler EndpointHandler) (*http.Server) {
mux := http.NewServeMux()
mux.Handle(endpoint.Path, handler)
srv := &http.Server{
Addr: endpoint.Address,
Handler: mux,
TLSConfig: endpoint.TLSConfig,
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler), 0),
}
return srv
}