-
Notifications
You must be signed in to change notification settings - Fork 0
/
mso_token.go
90 lines (76 loc) · 3.08 KB
/
mso_token.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
package msotoken
import (
"context"
"errors"
"fmt"
"time"
"github.com/AzureAD/microsoft-authentication-library-for-go/apps/confidential"
"github.com/patrickmn/go-cache"
)
// https://aad.portal.azure.com/ Azure Active Directory admin center
// https://docs.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-authenticate-an-ews-application-by-using-oauth
var (
// the tenant id from microsoft Azure Active Directory, ask from administrators
TenantID string
// the application client id to fetch token from microsoft online, ask from administrators
ClientID string
// the application client secret to fetch token from microsoft online, ask from administrators
ClientSecret string
// the permission scope required for microsoft EWS online
PermissionScopes []string = []string{"https://outlook.office365.com/.default"}
// the authority base url for microsoft EWS online
AuthorityBaseUrl string = "https://login.microsoftonline.com/"
// cache instance with a default expiration time of 5 minutes, and which purges expired items every 10 minutes
CacheIns *cache.Cache = cache.New(5*time.Minute, 10*time.Minute)
// the default cache time
DefaultCacheTime time.Duration = cache.DefaultExpiration
)
func GetToken() (string, error) {
if accessToken, found := CacheIns.Get("msalToken"); found && accessToken.(string) != "" {
fmt.Printf("get msalToken from cache: %v\n", accessToken)
return accessToken.(string), nil
}
if TenantID == "" {
fmt.Println("No valid tenant ID provided")
return "", errors.New("no valid tenant id provided")
}
if ClientID == "" {
fmt.Println("No valid client ID provided")
return "", errors.New("no valid client id provided")
}
if ClientSecret == "" {
fmt.Println("No valid client secret provided")
return "", errors.New("no valid client secret provided")
}
cred, err := confidential.NewCredFromSecret(ClientSecret)
if err != nil {
fmt.Printf("Create credential from a secret failed with error: %v\n", err)
return "", err
}
authApp, err := confidential.New(ClientID, cred, confidential.WithAuthority(AuthorityBaseUrl+TenantID))
if err != nil {
fmt.Printf("Create authentication application failed with error: %v\n", err)
return "", err
}
cacheToken := func(token string) {
if token != "" {
go CacheIns.Set("msalToken", token, DefaultCacheTime)
}
}
result, err := authApp.AcquireTokenSilent(context.Background(), PermissionScopes)
if err != nil {
fmt.Printf("Fetch silent token failed with error: %v\n", err)
fmt.Println("Trying to acquire token by credential......")
result, err = authApp.AcquireTokenByCredential(context.Background(), PermissionScopes)
if err != nil {
fmt.Printf("Acquire token by credential failed with error: %v\n", err)
return "", err
}
cacheToken(result.AccessToken) // cache the aquired token by credential
fmt.Println("Acquired access token by credential: " + result.AccessToken)
return result.AccessToken, nil
}
cacheToken(result.AccessToken) // cache the silently aquired token
fmt.Println("Silently acquired access token: " + result.AccessToken)
return result.AccessToken, nil
}