This repository has been archived by the owner on May 5, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth-msdc.go
65 lines (55 loc) · 1.47 KB
/
auth-msdc.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
package main
import (
"fmt"
"net/http"
"github.com/Azure/go-autorest/autorest/adal"
)
// MSDC settings
type MSDC struct {
tenant string
application string
client *http.Client
oauthConfig *adal.OAuthConfig
}
// NewMSDC configuration
func NewMSDC(tenant, application string) (*MSDC, error) {
oauthConfig, err := adal.NewOAuthConfig("https://login.microsoftonline.com/", tenant)
if err != nil {
return nil, err
}
return &MSDC{
tenant: tenant,
application: application,
client: &http.Client{},
oauthConfig: oauthConfig,
}, nil
}
// Callback checks if a token is still valid
func (msdc *MSDC) Callback(token adal.Token) error {
if token.IsExpired() {
return fmt.Errorf("Token has expired")
}
return nil
}
// Get acquires the device code
func (msdc *MSDC) Get(resource string) (*adal.DeviceCode, error) {
// Acquire the device code
return adal.InitiateDeviceAuth(
msdc.client,
*msdc.oauthConfig,
msdc.application,
resource)
}
// WaitForUserCompletion is waiting until the user is authenticated
func (msdc *MSDC) WaitForUserCompletion(deviceCode *adal.DeviceCode) (*adal.Token, error) {
return adal.WaitForUserCompletion(msdc.client, deviceCode)
}
// CheckResource if user has access to this resource
func (msdc *MSDC) CheckResource(token adal.Token, resource string) (*adal.ServicePrincipalToken, error) {
return adal.NewServicePrincipalTokenFromManualToken(
*msdc.oauthConfig,
msdc.application,
resource,
token,
msdc.Callback)
}