Skip to content

Commit

Permalink
converter
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Nov 20, 2022
1 parent e6492c1 commit 74aead6
Show file tree
Hide file tree
Showing 20 changed files with 605 additions and 160 deletions.
31 changes: 31 additions & 0 deletions auth/auth.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package auth

import (
"context"
"encoding/json"
"errors"
"io"
"net/http"
"runtime/trace"
"strings"

"github.com/slack-go/slack"
)

// Type is the auth type.
Expand All @@ -32,6 +36,8 @@ type Provider interface {
// Validate should return error, in case the token or cookies cannot be
// retrieved.
Validate() error
// Test tests if credentials are valid.
Test(ctx context.Context) error
}

var (
Expand Down Expand Up @@ -71,6 +77,14 @@ func deref[T any](cc []*T) []T {
return ret
}

func ref[T any](cc []T) []*T {
var ret = make([]*T, len(cc))
for i := range cc {
ret[i] = &cc[i]
}
return ret
}

// Load deserialises JSON data from reader and returns a ValueAuth, that can
// be used to authenticate Slackdump. It will return ErrNoToken or
// ErrNoCookie if the authentication information is missing.
Expand Down Expand Up @@ -107,3 +121,20 @@ func Save(w io.Writer, p Provider) error {
func IsClientToken(tok string) bool {
return strings.HasPrefix(tok, "xoxc-")
}

// TestAuth attempts to authenticate with the given provider. It will return
// AuthError if failed.
func (s simpleProvider) Test(ctx context.Context) error {
ctx, task := trace.NewTask(ctx, "TestAuth")
defer task.End()

cl := slack.New(s.Token, slack.OptionCookieRAW(ref(s.Cookie)...))

region := trace.StartRegion(ctx, "AuthTestContext")
defer region.End()
_, err := cl.AuthTestContext(ctx)
if err != nil {
return &Error{Err: err}
}
return nil
}
21 changes: 21 additions & 0 deletions auth/auth_error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package auth

import "fmt"

// Error is the error returned by New, the underlying Err contains
// an API error returned by slack.AuthTest call.
type Error struct {
Err error
}

func (ae *Error) Error() string {
return fmt.Sprintf("failed to authenticate: %s", ae.Err)
}

func (ae *Error) Unwrap() error {
return ae.Err
}

func (ae *Error) Is(target error) bool {
return target == ae.Err
}
6 changes: 3 additions & 3 deletions auth_error_test.go → auth/auth_error_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package slackdump
package auth

import (
"errors"
Expand Down Expand Up @@ -30,7 +30,7 @@ func TestAuthError_Unwrap(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ae := &AuthError{
ae := &Error{
Err: tt.fields.Err,
}
if err := ae.Unwrap(); (err != nil) && !errors.Is(err, tt.wantErr) {
Expand Down Expand Up @@ -68,7 +68,7 @@ func TestAuthError_Is(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ae := &AuthError{
ae := &Error{
Err: tt.fields.Err,
}
if got := ae.Is(tt.args.target); got != tt.want {
Expand Down
21 changes: 0 additions & 21 deletions auth_error.go

This file was deleted.

Loading

0 comments on commit 74aead6

Please sign in to comment.