|
| 1 | +// Copyright © 2025 Microsoft <[email protected]> |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +// of this software and associated documentation files (the "Software"), to deal |
| 5 | +// in the Software without restriction, including without limitation the rights |
| 6 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +// copies of the Software, and to permit persons to whom the Software is |
| 8 | +// furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in |
| 11 | +// all copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | +// THE SOFTWARE. |
| 20 | + |
| 21 | +package azcopy |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + "errors" |
| 26 | + "fmt" |
| 27 | + |
| 28 | + "github.com/Azure/azure-storage-azcopy/v10/common" |
| 29 | + "github.com/Azure/azure-storage-azcopy/v10/ste" |
| 30 | +) |
| 31 | + |
| 32 | +type LoginOptions common.LoginOptions |
| 33 | +type LoginResponse common.LoginResponse |
| 34 | + |
| 35 | +func (c *Client) Login(opts LoginOptions) (LoginResponse, error) { |
| 36 | + resp, err := c.oauthTokenManager.Login(common.LoginOptions(opts)) |
| 37 | + return LoginResponse(resp), err |
| 38 | +} |
| 39 | + |
| 40 | +type GetLoginStatusOptions struct { |
| 41 | +} |
| 42 | + |
| 43 | +type LoginStatus struct { |
| 44 | + Valid bool |
| 45 | + TenantID string |
| 46 | + AADEndpoint string |
| 47 | + LoginType common.AutoLoginType |
| 48 | +} |
| 49 | + |
| 50 | +func (c *Client) GetLoginStatus(_ GetLoginStatusOptions) (LoginStatus, error) { |
| 51 | + uotm := c.GetUserOAuthTokenManagerInstance() |
| 52 | + |
| 53 | + // Get current token info and refresh it with GetTokenInfo() |
| 54 | + ctx := context.WithValue(context.TODO(), ste.ServiceAPIVersionOverride, ste.DefaultServiceApiVersion) |
| 55 | + tokenInfo, err := uotm.GetTokenInfo(ctx) |
| 56 | + if err != nil || tokenInfo.IsExpired() { |
| 57 | + return LoginStatus{Valid: false}, errors.New("you are currently not logged in, please login using 'azcopy login'") |
| 58 | + } |
| 59 | + return LoginStatus{ |
| 60 | + Valid: true, |
| 61 | + TenantID: tokenInfo.Tenant, |
| 62 | + AADEndpoint: tokenInfo.ActiveDirectoryEndpoint, |
| 63 | + LoginType: tokenInfo.LoginType, |
| 64 | + }, nil |
| 65 | +} |
| 66 | + |
| 67 | +type LogoutOptions struct { |
| 68 | +} |
| 69 | + |
| 70 | +type LogoutResponse struct { |
| 71 | +} |
| 72 | + |
| 73 | +func (c *Client) Logout(_ LogoutOptions) (LogoutResponse, error) { |
| 74 | + uotm := c.GetUserOAuthTokenManagerInstance() |
| 75 | + if err := uotm.RemoveCachedToken(); err != nil { |
| 76 | + return LogoutResponse{}, fmt.Errorf("failed to perform logout command, %v", err) |
| 77 | + } |
| 78 | + return LogoutResponse{}, nil |
| 79 | +} |
0 commit comments