-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement httphandler in new webhok module. (#384)
- Loading branch information
Showing
4 changed files
with
235 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright 2016 LINE Corporation | ||
// | ||
// LINE Corporation licenses this file to you 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 webhook | ||
|
||
import ( | ||
"errors" | ||
"log" | ||
"net/http" | ||
) | ||
|
||
// EventsHandlerFunc type | ||
type EventsHandlerFunc func(*CallbackRequest, *http.Request) | ||
|
||
// ErrorHandlerFunc type | ||
type ErrorHandlerFunc func(error, *http.Request) | ||
|
||
// WebhookHandler type | ||
type WebhookHandler struct { | ||
channelSecret string | ||
|
||
handleEvents EventsHandlerFunc | ||
handleError ErrorHandlerFunc | ||
} | ||
|
||
// New returns a new WebhookHandler instance. | ||
func NewWebhookHandler(channelSecret string) (*WebhookHandler, error) { | ||
if channelSecret == "" { | ||
return nil, errors.New("missing channel secret") | ||
} | ||
h := &WebhookHandler{ | ||
channelSecret: channelSecret, | ||
} | ||
return h, nil | ||
} | ||
|
||
// HandleEvents method | ||
func (wh *WebhookHandler) HandleEvents(f EventsHandlerFunc) { | ||
wh.handleEvents = f | ||
} | ||
|
||
// HandleError method | ||
func (wh *WebhookHandler) HandleError(f ErrorHandlerFunc) { | ||
wh.handleError = f | ||
} | ||
|
||
func (wh *WebhookHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
events, err := ParseRequest(wh.channelSecret, r) | ||
if err != nil { | ||
if wh.handleError != nil { | ||
wh.handleError(err, r) | ||
} | ||
if err == ErrInvalidSignature { | ||
log.Printf("linebot webhook request validation error: %v", err) | ||
w.WriteHeader(400) | ||
} else { | ||
log.Printf("linebot internal server error: %v", err) | ||
w.WriteHeader(500) | ||
} | ||
return | ||
} | ||
wh.handleEvents(events, r) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// Copyright 2016 LINE Corporation | ||
// | ||
// LINE Corporation licenses this file to you 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 webhook | ||
|
||
import ( | ||
"bytes" | ||
"crypto/hmac" | ||
"crypto/sha256" | ||
"crypto/tls" | ||
"encoding/base64" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
var testRequestBody = `{ | ||
"events": [ | ||
{ | ||
"replyToken": "nHuyWiB7yP5Zw52FIkcQobQuGDXCTA", | ||
"type": "message", | ||
"timestamp": 1462629479859, | ||
"source": { | ||
"type": "user", | ||
"userId": "u206d25c2ea6bd87c17655609a1c37cb8" | ||
}, | ||
"message": { | ||
"id": "325708", | ||
"type": "text", | ||
"text": "Hello, world" | ||
} | ||
} | ||
] | ||
} | ||
` | ||
|
||
const ( | ||
testChannelSecret = "testsecret" | ||
testChannelToken = "testtoken" | ||
) | ||
|
||
func TestWebhookHandler(t *testing.T) { | ||
handler, err := NewWebhookHandler(testChannelSecret) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
handlerFunc := func(req *CallbackRequest, r *http.Request) { | ||
if req == nil { | ||
t.Errorf("events is nil") | ||
} | ||
if r == nil { | ||
t.Errorf("r is nil") | ||
} | ||
} | ||
handler.HandleEvents(handlerFunc) | ||
|
||
server := httptest.NewTLSServer(handler) | ||
defer server.Close() | ||
httpClient := &http.Client{ | ||
Transport: &http.Transport{ | ||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, | ||
}, | ||
} | ||
|
||
// valid signature | ||
{ | ||
body := []byte(testRequestBody) | ||
req, err := http.NewRequest("POST", server.URL, bytes.NewReader(body)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
// generate signature | ||
mac := hmac.New(sha256.New, []byte(testChannelSecret)) | ||
mac.Write(body) | ||
|
||
req.Header.Set("X-Line-Signature", base64.StdEncoding.EncodeToString(mac.Sum(nil))) | ||
res, err := httpClient.Do(req) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if res == nil { | ||
t.Fatal("response is nil") | ||
} | ||
if res.StatusCode != http.StatusOK { | ||
t.Errorf("status: %d", res.StatusCode) | ||
} | ||
} | ||
|
||
// invalid signature | ||
handler.HandleError(func(err error, r *http.Request) { | ||
if err != ErrInvalidSignature { | ||
t.Errorf("err %v; want %v", err, ErrInvalidSignature) | ||
} | ||
if r == nil { | ||
t.Errorf("r is nil") | ||
} | ||
}) | ||
{ | ||
body := []byte(testRequestBody) | ||
req, err := http.NewRequest("POST", server.URL, bytes.NewReader(body)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
req.Header.Set("X-LINE-Signature", "invalidSignature") | ||
res, err := httpClient.Do(req) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if res == nil { | ||
t.Fatal("response is nil") | ||
} | ||
if res.StatusCode != 400 { | ||
t.Errorf("status: %d", 400) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters