Skip to content

Commit

Permalink
fix: reformat, add some docs
Browse files Browse the repository at this point in the history
  • Loading branch information
tdakkota committed Feb 27, 2020
1 parent 1c5f9c8 commit 8201eaa
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions api/authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@ package api
import (
"bytes"
"encoding/json"
"github.com/SevereCloud/vksdk/api/errors"
"net/http"

"github.com/SevereCloud/vksdk/api/errors"
)

// Authenticator interface
type Authenticator interface {
Authenticate(params Params) error
}

// AuthenticatorFunc func
type AuthenticatorFunc func(params Params) error

// Authenticate method
// Implements Authenticator for AuthenticatorFunc
func (f AuthenticatorFunc) Authenticate(params Params) error {
return f(params)
}
Expand All @@ -31,7 +36,7 @@ func TokenAuthenticator(token string) Authenticator {

// AuthCodeFlowRequest struct
type AuthCodeFlowRequest struct {
ClientId int `json:"client_id"`
ClientID int `json:"client_id"`
ClientSecret string `json:"client_secret"`
RedirectURL string `json:"redirect_url"`
Code string `json:"code"`
Expand Down Expand Up @@ -60,7 +65,12 @@ func authCodeFlow(auth AuthCodeFlowRequest) (token TokenResponse, err error) {
return
}

if resp.Body != nil {
defer resp.Body.Close()
}

var response Response

err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
return
Expand All @@ -72,13 +82,15 @@ func authCodeFlow(auth AuthCodeFlowRequest) (token TokenResponse, err error) {
}

err = json.Unmarshal(response.Response, &token)
return

return token, err
}

// CodeAuthenticator func
// Authorization code flow implementation
func CodeAuthenticator(auth AuthCodeFlowRequest) (Authenticator, error) {
r, err := authCodeFlow(auth)

return AuthenticatorFunc(func(params Params) error {
if _, ok := params["access_token"]; !ok {
params["access_token"] = r.AccessToken
Expand Down

0 comments on commit 8201eaa

Please sign in to comment.