|
| 1 | +package login |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/base64" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "reflect" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/scaleway/scaleway-cli/v2/internal/core" |
| 12 | + initCommand "github.com/scaleway/scaleway-cli/v2/internal/namespaces/init" |
| 13 | + "github.com/scaleway/scaleway-cli/v2/internal/namespaces/login/webcallback" |
| 14 | + iam "github.com/scaleway/scaleway-sdk-go/api/iam/v1alpha1" |
| 15 | + "github.com/scaleway/scaleway-sdk-go/logger" |
| 16 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 17 | + "github.com/skratchdot/open-golang/open" |
| 18 | +) |
| 19 | + |
| 20 | +func GetCommands() *core.Commands { |
| 21 | + return core.NewCommands(loginCommand()) |
| 22 | +} |
| 23 | + |
| 24 | +type loginArgs struct { |
| 25 | + Port int `json:"port"` |
| 26 | + // PrintURL will print the account url instead of trying to open it with a browser |
| 27 | + PrintURL bool `json:"print_url"` |
| 28 | +} |
| 29 | + |
| 30 | +func loginCommand() *core.Command { |
| 31 | + return &core.Command{ |
| 32 | + Groups: []string{"config"}, |
| 33 | + Short: `Login to scaleway`, |
| 34 | + Long: `Start an interactive connection to scaleway to initialize the active profile of the config |
| 35 | +A webpage will open while the CLI will wait for a response. |
| 36 | +Once you connected to Scaleway, the profile should be configured. |
| 37 | +`, |
| 38 | + Namespace: "login", |
| 39 | + AllowAnonymousClient: true, |
| 40 | + ArgsType: reflect.TypeOf(loginArgs{}), |
| 41 | + ArgSpecs: core.ArgSpecs{ |
| 42 | + { |
| 43 | + Name: "port", |
| 44 | + Short: "The port number used to wait for browser's response", |
| 45 | + }, |
| 46 | + }, |
| 47 | + SeeAlsos: []*core.SeeAlso{ |
| 48 | + { |
| 49 | + Short: "Init profile manually", |
| 50 | + Command: "scw init", |
| 51 | + }, |
| 52 | + }, |
| 53 | + Run: func(ctx context.Context, argsI interface{}) (interface{}, error) { |
| 54 | + args := argsI.(*loginArgs) |
| 55 | + |
| 56 | + opts := []webcallback.Options(nil) |
| 57 | + if args.Port > 0 { |
| 58 | + opts = append(opts, webcallback.WithPort(args.Port)) |
| 59 | + } |
| 60 | + |
| 61 | + wb := webcallback.New(opts...) |
| 62 | + err := wb.Start() |
| 63 | + if err != nil { |
| 64 | + return nil, err |
| 65 | + } |
| 66 | + |
| 67 | + callbackURL := fmt.Sprintf("http://localhost:%d/callback", wb.Port()) |
| 68 | + |
| 69 | + accountURL := "https://account.scaleway.com/authenticate?redirectToUrl=" + callbackURL |
| 70 | + |
| 71 | + logger.Debugf("Web server started, waiting for callback on %s\n", callbackURL) |
| 72 | + |
| 73 | + if args.PrintURL { |
| 74 | + fmt.Println(accountURL) |
| 75 | + } else { |
| 76 | + err = open.Start(accountURL) |
| 77 | + if err != nil { |
| 78 | + logger.Warningf("Failed to open web url, you may not have a default browser configured") |
| 79 | + logger.Warningf("You can open it: " + accountURL) |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + fmt.Println("waiting for callback from browser...") |
| 84 | + token, err := wb.Wait(ctx) |
| 85 | + if err != nil { |
| 86 | + return nil, err |
| 87 | + } |
| 88 | + |
| 89 | + rawToken, err := base64.StdEncoding.DecodeString(token) |
| 90 | + if err != nil { |
| 91 | + return nil, err |
| 92 | + } |
| 93 | + |
| 94 | + tt := Token{} |
| 95 | + |
| 96 | + err = json.Unmarshal(rawToken, &tt) |
| 97 | + if err != nil { |
| 98 | + return nil, err |
| 99 | + } |
| 100 | + |
| 101 | + client, err := scw.NewClient(scw.WithJWT(tt.Token)) |
| 102 | + if err != nil { |
| 103 | + return nil, err |
| 104 | + } |
| 105 | + |
| 106 | + api := iam.NewAPI(client) |
| 107 | + apiKey, err := api.CreateAPIKey(&iam.CreateAPIKeyRequest{ |
| 108 | + UserID: &tt.Jwt.AudienceID, |
| 109 | + Description: "Generated by the Scaleway CLI", |
| 110 | + }) |
| 111 | + if err != nil { |
| 112 | + return nil, err |
| 113 | + } |
| 114 | + |
| 115 | + resp, err := initCommand.Command().Run(ctx, &initCommand.Args{ |
| 116 | + AccessKey: apiKey.AccessKey, |
| 117 | + SecretKey: *apiKey.SecretKey, |
| 118 | + ProjectID: apiKey.DefaultProjectID, |
| 119 | + OrganizationID: apiKey.DefaultProjectID, |
| 120 | + Region: scw.RegionFrPar, |
| 121 | + Zone: scw.ZoneFrPar1, |
| 122 | + }) |
| 123 | + if err != nil { |
| 124 | + // Cleanup API Key if init failed |
| 125 | + logger.Warningf("Init failed, cleaning API key.\n") |
| 126 | + cleanErr := api.DeleteAPIKey(&iam.DeleteAPIKeyRequest{ |
| 127 | + AccessKey: apiKey.AccessKey, |
| 128 | + }) |
| 129 | + if cleanErr != nil { |
| 130 | + logger.Warningf("Failed to clean API key: %s\n", err.Error()) |
| 131 | + } |
| 132 | + return nil, err |
| 133 | + } |
| 134 | + |
| 135 | + return resp, nil |
| 136 | + }, |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +type Token struct { |
| 141 | + Jwt struct { |
| 142 | + AudienceID string `json:"audienceId"` |
| 143 | + CreatedAt time.Time `json:"createdAt"` |
| 144 | + ExpiresAt time.Time `json:"expiresAt"` |
| 145 | + IP string `json:"ip"` |
| 146 | + IssuerID string `json:"issuerId"` |
| 147 | + Jti string `json:"jti"` |
| 148 | + UpdatedAt time.Time `json:"updatedAt"` |
| 149 | + UserAgent string `json:"userAgent"` |
| 150 | + } `json:"jwt"` |
| 151 | + RenewToken string `json:"renewToken"` |
| 152 | + Token string `json:"token"` |
| 153 | +} |
0 commit comments