Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use base64.RawURLEncoding to decode signed request data. #62

Merged
merged 1 commit into from
May 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Use base64.RawURLEncoding to decode signed request data.
  • Loading branch information
zonr committed May 6, 2017
commit f726252f1fa60cf91bdd6ef30197be32a5ae9f10
5 changes: 3 additions & 2 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"strings"
Expand Down Expand Up @@ -38,14 +39,14 @@ func (app *App) ParseSignedRequest(signedRequest string) (res Result, err error)
return
}

sig, e1 := decodeBase64URLEncodingString(strs[0])
sig, e1 := base64.RawURLEncoding.DecodeString(strs[0])

if e1 != nil {
err = fmt.Errorf("cannot decode signed request sig. error is %v.", e1)
return
}

payload, e2 := decodeBase64URLEncodingString(strs[1])
payload, e2 := base64.RawURLEncoding.DecodeString(strs[1])

if e2 != nil {
err = fmt.Errorf("cannot decode signed request payload. error is %v.", e2)
Expand Down
21 changes: 0 additions & 21 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
)

// Makes a facebook graph api call.
Expand Down Expand Up @@ -649,22 +647,3 @@ func (session *Session) addDebugInfo(res Result, response *http.Response) Result
res["__debug__"] = debugInfo
return res
}

func decodeBase64URLEncodingString(data string) ([]byte, error) {
buf := bytes.NewBufferString(data)

// go's URLEncoding implementation requires base64 padding.
if m := len(data) % 4; m != 0 {
buf.WriteString(strings.Repeat("=", 4-m))
}

reader := base64.NewDecoder(base64.URLEncoding, buf)
output := &bytes.Buffer{}
_, err := io.Copy(output, reader)

if err != nil {
return nil, err
}

return output.Bytes(), nil
}