-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathloginStatus.go
115 lines (100 loc) · 4.09 KB
/
loginStatus.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright © Microsoft <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package cmd
import (
"context"
"encoding/json"
"fmt"
"github.com/Azure/azure-storage-azcopy/v10/common"
"github.com/Azure/azure-storage-azcopy/v10/ste"
"github.com/spf13/cobra"
)
type LoginStatusOutput struct {
Valid bool `json:"valid"`
TenantID *string `json:"tenantID,omitempty"`
AADEndpoint *string `json:"AADEndpoint,omitempty"`
AuthMethod *string `json:"authMethod,omitempty"`
}
func init() {
type loginStatus struct {
tenantID bool
endpoint bool
method bool
}
commandLineInput := loginStatus{}
lgStatus := &cobra.Command{
Use: "status",
Short: loginStatusShortDescription,
Long: loginStatusLongDescription,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
return fmt.Errorf("login status does not require any argument")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
// getting current token info and refreshing it with GetTokenInfo()
ctx := context.WithValue(context.TODO(), ste.ServiceAPIVersionOverride, ste.DefaultServiceApiVersion)
uotm := GetUserOAuthTokenManagerInstance()
tokenInfo, err := uotm.GetTokenInfo(ctx)
var Info = LoginStatusOutput{
Valid: err == nil && !tokenInfo.IsExpired(),
}
logText := func(format string, a ...any) {
if azcopyOutputFormat == common.EOutputFormat.None() || azcopyOutputFormat == common.EOutputFormat.Text() {
glcm.Info(fmt.Sprintf(format, a...))
}
}
if Info.Valid {
logText("You have successfully refreshed your token. Your login session is still active")
if commandLineInput.tenantID {
logText("Tenant ID: %v", tokenInfo.Tenant)
Info.TenantID = &tokenInfo.Tenant
}
if commandLineInput.endpoint {
logText(fmt.Sprintf("Active directory endpoint: %v", tokenInfo.ActiveDirectoryEndpoint))
Info.AADEndpoint = &tokenInfo.ActiveDirectoryEndpoint
}
if commandLineInput.method {
logText(fmt.Sprintf("Authorized using %s", tokenInfo.LoginType))
method := tokenInfo.LoginType.String()
Info.AuthMethod = &method
}
} else {
logText("You are currently not logged in. Please login using 'azcopy login'")
}
if azcopyOutputFormat == common.EOutputFormat.Json() {
glcm.Output(
func(_ common.OutputFormat) string {
buf, err := json.Marshal(Info)
if err != nil {
panic(err)
}
return string(buf)
}, common.EOutputMessageType.LoginStatusInfo())
}
glcm.Exit(nil, common.Iff(Info.Valid, common.EExitCode.Success(), common.EExitCode.Error()))
},
}
lgCmd.AddCommand(lgStatus)
lgStatus.PersistentFlags().BoolVar(&commandLineInput.tenantID, "tenant", false, "Prints the Microsoft Entra tenant ID that is currently being used in session.")
lgStatus.PersistentFlags().BoolVar(&commandLineInput.endpoint, "endpoint", false, "Prints the Microsoft Entra endpoint that is being used in the current session.")
lgStatus.PersistentFlags().BoolVar(&commandLineInput.method, "method", false, "Prints the authorization method used in the current session.")
}