Skip to content

Commit

Permalink
feat: add oauth module
Browse files Browse the repository at this point in the history
feat(oauth): scope

test: add t.Parallel()

feat: add oauth module
  • Loading branch information
SevereCloud committed Jan 9, 2022
1 parent bd2de0d commit 869811a
Show file tree
Hide file tree
Showing 11 changed files with 1,641 additions and 0 deletions.
1 change: 1 addition & 0 deletions .markdownlint.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
---
no-hard-tabs: false
no-duplicate-heading: false
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Version API 5.131.
- Ability to modify HTTP client
- Request Limiter
- Token pool
- [OAuth](https://pkg.go.dev/github.com/SevereCloud/vksdk/v2/api/oauth)
- [Callback API](https://pkg.go.dev/github.com/SevereCloud/vksdk/v2/callback)
- Tracking tool for users activity in your VK communities
- Supports all events
Expand Down
430 changes: 430 additions & 0 deletions api/oauth/README.md

Large diffs are not rendered by default.

96 changes: 96 additions & 0 deletions api/oauth/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Package oauth ...
package oauth // import "github.com/SevereCloud/vksdk/v2/api/oauth"

import "errors"

// ValidationType ...
type ValidationType string

// Possible values.
const (
ValidationSMS ValidationType = "2fa_sms"
ValidationApp ValidationType = "2fa_app"
)

// ErrorType for oauth.
type ErrorType string

// Error types.
//
// See https://tools.ietf.org/html/rfc6749#section-4.2.2.1
const (
ErrInvalidRequest ErrorType = "invalid_request"
ErrUnauthorizedClient ErrorType = "unauthorized_client"
ErrUnsupportedResponseType ErrorType = "unsupported_response_type"
ErrInvalidScope ErrorType = "invalid_scope"
ErrServerError ErrorType = "server_error"
ErrTemporarilyUnavailable ErrorType = "temporarily_unavailable"
ErrAccessDenied ErrorType = "access_denied"

ErrInvalidGrant ErrorType = "invalid_grant"

ErrNeedValidation ErrorType = "need_validation"
ErrNeedCaptcha ErrorType = "need_captcha"
)

// Error returns the message of a Error.
func (e ErrorType) Error() string {
return "oauth: error with type " + string(e)
}

// ErrorReason for oauth.
type ErrorReason string

// Error returns the message of a Error.
func (e ErrorReason) Error() string {
return "oauth: error with reason " + string(e)
}

// ErrorReason types.
const (
ErrUserDenied ErrorReason = "user_denied"
)

// Error for oauth.
type Error struct {
Type ErrorType `json:"error"`
Reason ErrorReason `json:"error_reason,omitempty"`
Description string `json:"error_description,omitempty"`

// For auth direct
CaptchaSID string `json:"captcha_sid,omitempty"`
CaptchaImg string `json:"captcha_img,omitempty"`
RedirectURI string `json:"redirect_uri,omitempty"`
ValidationType ValidationType `json:"validation_type,omitempty"`
PhoneMask string `json:"phone_mask,omitempty"`
}

// Error returns the message of a Error.
func (e Error) Error() string {
if e.Description != "" {
return "oauth: " + e.Description
}

return e.Type.Error()
}

// Is unwraps its first argument sequentially looking for an error that matches
// the second.
func (e Error) Is(target error) bool {
var tError *Error
if errors.As(target, &tError) {
return e.Type == tError.Type && e.Description == tError.Description
}

var tErrorType ErrorType
if errors.As(target, &tErrorType) {
return e.Type == tErrorType
}

var tErrorReason ErrorReason
if errors.As(target, &tErrorReason) {
return e.Reason == tErrorReason
}

return false
}
76 changes: 76 additions & 0 deletions api/oauth/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package oauth_test

import (
"errors"
"testing"

"github.com/SevereCloud/vksdk/v2/api/oauth"
"github.com/stretchr/testify/assert"
)

type otherError string

func (e otherError) Error() string {
return string(e)
}

func TestErrorType(t *testing.T) {
t.Parallel()

err := oauth.ErrorType("aoa")
assert.EqualError(t, err, "oauth: error with type aoa")
}

func TestErrorReason(t *testing.T) {
t.Parallel()

err := oauth.ErrorReason("aoa")
assert.EqualError(t, err, "oauth: error with reason aoa")
}

func TestError_Error(t *testing.T) {
t.Parallel()

err := oauth.Error{
Type: oauth.ErrorType("aoa"),
Description: "test message",
}
assert.EqualError(t, err, "oauth: test message")

err = oauth.Error{
Type: oauth.ErrorType("aoa"),
}
assert.EqualError(t, err, "oauth: error with type aoa")
}

func TestError_Is(t *testing.T) {
t.Parallel()

f := func(err *oauth.Error, target error, want bool) {
t.Helper()

assert.Equal(t, want, errors.Is(err, target))
}

f(&oauth.Error{Type: oauth.ErrorType("aoa")}, &oauth.Error{Type: oauth.ErrorType("aoa")}, true)
f(&oauth.Error{Type: oauth.ErrAccessDenied}, oauth.ErrAccessDenied, true)
f(&oauth.Error{Reason: oauth.ErrUserDenied}, oauth.ErrUserDenied, true)
f(&oauth.Error{Type: oauth.ErrorType("aoa"), Description: "123"}, &oauth.Error{Type: oauth.ErrorType("aoa"), Description: "123"}, true)

f(&oauth.Error{Type: oauth.ErrorType("aoa")}, &oauth.Error{Type: oauth.ErrorType("oao")}, false)
f(&oauth.Error{Type: oauth.ErrorType("aoa")}, oauth.ErrorType("oao"), false)
f(&oauth.Error{Reason: oauth.ErrorReason("aoa")}, oauth.ErrorReason("oao"), false)
f(&oauth.Error{Type: oauth.ErrorType("aoa"), Description: "123"}, &oauth.Error{Type: oauth.ErrorType("aoa"), Description: "321"}, false)
f(&oauth.Error{Type: oauth.ErrorType("aoa")}, otherError("test"), false)
}

func TestError_As(t *testing.T) {
t.Parallel()

var target *oauth.Error

err := &oauth.Error{Type: oauth.ErrorType("aoa")}
if !errors.As(err, &target) && target.Type == "aoa" {
t.Error("As not working")
}
}

0 comments on commit 869811a

Please sign in to comment.