-
Notifications
You must be signed in to change notification settings - Fork 168
/
public.go
90 lines (71 loc) · 2.71 KB
/
public.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package auth
import (
"context"
"github.com/AzureAD/microsoft-authentication-library-for-go/apps/public"
)
// publicClient looks like a subset of the public.Client surface area, with small tweaks, to aid testing.
type publicClient interface {
Accounts(ctx context.Context) ([]public.Account, error)
RemoveAccount(ctx context.Context, account public.Account) error
AcquireTokenInteractive(context.Context, []string, ...public.AcquireInteractiveOption) (public.AuthResult, error)
AcquireTokenByDeviceCode(context.Context, []string, ...public.AcquireByDeviceCodeOption) (deviceCodeResult, error)
AcquireTokenSilent(context.Context, []string, ...public.AcquireSilentOption) (public.AuthResult, error)
}
type deviceCodeResult interface {
Message() string
UserCode() string
AuthenticationResult(context.Context) (public.AuthResult, error)
}
type msalPublicClientAdapter struct {
client *public.Client
}
func (m *msalPublicClientAdapter) Accounts(ctx context.Context) ([]public.Account, error) {
return m.client.Accounts(ctx)
}
func (m *msalPublicClientAdapter) RemoveAccount(ctx context.Context, account public.Account) error {
return m.client.RemoveAccount(ctx, account)
}
func (m *msalPublicClientAdapter) AcquireTokenInteractive(
ctx context.Context, scopes []string, options ...public.AcquireInteractiveOption,
) (public.AuthResult, error) {
res, err := m.client.AcquireTokenInteractive(ctx, scopes, options...)
if err != nil {
return res, newAuthFailedErrorFromMsalErr(err)
}
return res, nil
}
func (m *msalPublicClientAdapter) AcquireTokenByDeviceCode(
ctx context.Context, scopes []string, options ...public.AcquireByDeviceCodeOption) (deviceCodeResult, error) {
code, err := m.client.AcquireTokenByDeviceCode(ctx, scopes, options...)
if err != nil {
return nil, newAuthFailedErrorFromMsalErr(err)
}
return &msalDeviceCodeAdapter{code: &code}, nil
}
func (m *msalPublicClientAdapter) AcquireTokenSilent(
ctx context.Context, scopes []string, options ...public.AcquireSilentOption,
) (public.AuthResult, error) {
res, err := m.client.AcquireTokenSilent(ctx, scopes, options...)
if err != nil {
return res, newAuthFailedErrorFromMsalErr(err)
}
return res, nil
}
type msalDeviceCodeAdapter struct {
code *public.DeviceCode
}
func (m *msalDeviceCodeAdapter) Message() string {
return m.code.Result.Message
}
func (m *msalDeviceCodeAdapter) UserCode() string {
return m.code.Result.UserCode
}
func (m *msalDeviceCodeAdapter) AuthenticationResult(ctx context.Context) (public.AuthResult, error) {
res, err := m.code.AuthenticationResult(ctx)
if err != nil {
return res, newAuthFailedErrorFromMsalErr(err)
}
return res, nil
}