-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
86 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,26 @@ | ||
package kraken | ||
|
||
import "context" | ||
|
||
// Secret represents a Kraken API secret. | ||
type Secret []byte | ||
|
||
// APIKey represents a Kraken API key. | ||
type APIKey string | ||
|
||
// Otp defines a single-use password. | ||
// It is used as a second authentication factor. | ||
type Otp string | ||
|
||
type otpKey string | ||
|
||
// OtpFromContext returns the one-time password from the context. | ||
func OtpFromContext(ctx context.Context) Otp { | ||
otp, _ := ctx.Value(otpKey("otp")).(Otp) | ||
return otp | ||
} | ||
|
||
// ContextWithOtp adds a one-time password to the context. | ||
func ContextWithOtp(ctx context.Context, otp Otp) context.Context { | ||
return context.WithValue(ctx, otpKey("otp"), otp) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package kraken | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
) | ||
|
||
func TestOtpFromContext(t *testing.T) { | ||
type args struct { | ||
ctx context.Context | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want Otp | ||
}{ | ||
{ | ||
name: "empty context", | ||
args: args{ctx: context.Background()}, | ||
want: "", | ||
}, | ||
{ | ||
name: "context with otp", | ||
args: args{ctx: ContextWithOtp(context.Background(), "123456")}, | ||
want: "123456", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := OtpFromContext(tt.args.ctx); got != tt.want { | ||
t.Errorf("OtpFromContext() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters