Skip to content

Commit

Permalink
Refactor auth
Browse files Browse the repository at this point in the history
  • Loading branch information
utahta committed Nov 21, 2017
1 parent 7cdbedf commit 05fbaad
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 43 deletions.
8 changes: 7 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,8 @@
[[constraint]]
branch = "master"
name = "github.com/utahta/go-openuri"

[[constraint]]
branch = "master"
name = "github.com/google/uuid"

62 changes: 30 additions & 32 deletions auth.go → auth/auth.go
Original file line number Diff line number Diff line change
@@ -1,43 +1,48 @@
package linenotify
package auth

import (
"crypto/rand"
"encoding/base64"
"net/http"
"net/url"

"github.com/google/uuid"
"github.com/pkg/errors"
)

type AuthorizationClient struct {
ClientID string
RedirectURI string
ResponseMode string
State string
}
type (
// Client represents LINE Notify authorization
Client struct {
ClientID string
RedirectURI string
ResponseMode string
State string
}

type AuthorizationResponse struct {
Code string
State string
Error string
ErrorDescription string
}
// AuthorizeResponse represents LINE Notify authorize response
AuthorizeResponse struct {
Code string
State string
Error string
ErrorDescription string
}
)

func NewAuthorization(clientID, redirectURI string) (*AuthorizationClient, error) {
state, err := generateHash()
// New returns Client
func New(clientID, redirectURI string) (*Client, error) {
randomID, err := uuid.NewRandom()
if err != nil {
return nil, err
}

return &AuthorizationClient{
return &Client{
ClientID: clientID,
RedirectURI: redirectURI,
ResponseMode: "form_post",
State: state,
State: randomID.String(),
}, nil
}

func (c *AuthorizationClient) RequestURL() (string, error) {
// RequestURL builds request url with parameters
func (c *Client) RequestURL() (string, error) {
u, err := url.Parse("https://notify-bot.line.me/oauth/authorize")
if err != nil {
return "", err
Expand All @@ -55,7 +60,8 @@ func (c *AuthorizationClient) RequestURL() (string, error) {
return u.String(), nil
}

func (c *AuthorizationClient) Redirect(w http.ResponseWriter, req *http.Request) error {
// Redirect redirect to request url
func (c *Client) Redirect(w http.ResponseWriter, req *http.Request) error {
urlStr, err := c.RequestURL()
if err != nil {
return err
Expand All @@ -64,17 +70,9 @@ func (c *AuthorizationClient) Redirect(w http.ResponseWriter, req *http.Request)
return nil
}

func generateHash() (string, error) {
b := make([]byte, 32)
_, err := rand.Read(b)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(b), nil
}

func ParseAuthorization(r *http.Request) (*AuthorizationResponse, error) {
resp := &AuthorizationResponse{
// ParseAuthorize parses authorize request
func ParseAuthorize(r *http.Request) (*AuthorizeResponse, error) {
resp := &AuthorizeResponse{
Code: r.FormValue("code"),
State: r.FormValue("state"),
Error: r.FormValue("error"),
Expand Down
16 changes: 8 additions & 8 deletions auth_test.go → auth/auth_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package linenotify
package auth

import (
"net/http"
"net/url"
"testing"
)

func TestNewAuthorization(t *testing.T) {
req, err := NewAuthorization("id", "http://localhost")
func TestNew(t *testing.T) {
req, err := New("id", "http://localhost")
if err != nil {
t.Fatal(err)
}
Expand All @@ -16,13 +16,13 @@ func TestNewAuthorization(t *testing.T) {
t.Errorf("Expect form_post, got %v", req.ResponseMode)
}

if len(req.State) != 44 {
if len(req.State) != 36 {
t.Errorf("Expect state length 44, got %v", len(req.State))
}
}

func TestAuthorization_RequestURL(t *testing.T) {
req, err := NewAuthorization("id", "http://localhost/linenotify_test")
func TestClient_RequestURL(t *testing.T) {
req, err := New("id", "http://localhost/linenotify_test")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -57,7 +57,7 @@ func TestAuthorization_RequestURL(t *testing.T) {
}
}

func TestParseAuthorization(t *testing.T) {
func TestParseAuthorize(t *testing.T) {

tests := []struct {
urlValues url.Values
Expand All @@ -80,7 +80,7 @@ func TestParseAuthorization(t *testing.T) {

for _, test := range tests {
req := &http.Request{Form: test.urlValues}
resp, err := ParseAuthorization(req)
resp, err := ParseAuthorize(req)

if test.expectError {
if err == nil {
Expand Down
5 changes: 3 additions & 2 deletions notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/url"
"strings"

"github.com/google/uuid"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -100,12 +101,12 @@ func (c *Client) requestBodyWithImage(message string, image io.Reader) (io.Reade
return nil, "", err
}

filename, err := generateHash()
randomID, err := uuid.NewRandom()
if err != nil {
return nil, "", err
}

fw, err := w.CreateFormFile("imageFile", filename)
fw, err := w.CreateFormFile("imageFile", randomID.String())
if err != nil {
return nil, "", err
}
Expand Down

0 comments on commit 05fbaad

Please sign in to comment.