login

package
v0.21.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 4, 2024 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AddCmd = &cobra.Command{
	Use:   "add",
	Short: "Add or update a login profile",
	Long: `Add (or update) a login profile, for example:
mim login add --server="http://localhost:4242" --token="....secret token here..." --alias="local"
or
mim login add -s https://api.mimiro.io -a prod --clientId="..." --clientSecret="..."
`,
	Run: func(cmd *cobra.Command, args []string) {
		driver := display.ResolveDriver(cmd)
		pterm.EnableDebugMessages()

		server, _ := cmd.Flags().GetString("server")
		if server == "" {
			driver.RenderError(errors.New("missing server name"), true)
		}

		token, err := cmd.Flags().GetString("token")
		driver.RenderError(err, true)

		alias, err := cmd.Flags().GetString("alias")
		driver.RenderError(err, true)
		if alias == "" {
			alias = server
		}

		loginType, err := cmd.Flags().GetString("type")
		driver.RenderError(err, true)
		if loginType == "" {
			driver.RenderError(eris.New("you must set a login type. ie. --type token|user|client|cert|unsecured|admin"), true)
		}

		data := &config.Config{
			Server:       server,
			Token:        "",
			ClientId:     "",
			ClientSecret: "",
			Authorizer:   "",
			Type:         loginType,
		}

		switch loginType {
		case "admin":
			clientId, _ := cmd.Flags().GetString("clientId")
			clientSecret, _ := cmd.Flags().GetString("clientSecret")
			data.ClientId = clientId
			data.ClientSecret = clientSecret
		case "cert":
			clientId, _ := cmd.Flags().GetString("clientId")
			audience, _ := cmd.Flags().GetString("audience")
			data.ClientId = clientId
			data.Audience = audience
		case "client":
			clientId, _ := cmd.Flags().GetString("clientId")
			clientSecret, _ := cmd.Flags().GetString("clientSecret")
			authorizer, _ := cmd.Flags().GetString("authorizer")
			audience, _ := cmd.Flags().GetString("audience")
			if clientSecret == "" {
				driver.RenderError(errors.New("missing client secret"), true)
			}
			if authorizer == "" {
				driver.RenderError(errors.New("missing authorizer url"), true)
			}
			data.ClientId = clientId
			data.ClientSecret = clientSecret
			data.Authorizer = authorizer
			data.Audience = audience
		case "user":

			authorizer, err := cmd.Flags().GetString("authorizer")
			driver.RenderError(err, true)
			audience, err := cmd.Flags().GetString("audience")
			driver.RenderError(err, true)
			clientId, err := cmd.Flags().GetString("clientId")
			driver.RenderError(err, true)

			data.Authorizer = authorizer
			data.ClientId = clientId
			data.Audience = audience
		case "token":
			data.Token = token
		case "unsecured":
		default:
			driver.RenderError(errors.New("invalid type. Allowed types are: token|user|client|cert|unsecured|admin"), true)
		}

		err = config.Store(alias, data)
		driver.RenderError(err, true)

		driver.Msg("Login added to keyring", "")
	},
	TraverseChildren: true,
}

addCmd represents the add command

View Source
var CopyCmd = &cobra.Command{
	Use:   "copy",
	Short: "Copy a login profile",
	Long: `Copies an existing login profile to a new one, keeping its values. For example:
mim login copy my-alias --to new-alias
`,
	Run: func(cmd *cobra.Command, args []string) {
		pterm.EnableDebugMessages()

		alias, err := cmd.Flags().GetString("alias")
		utils.HandleError(err)

		if alias == "" && len(args) > 0 {
			alias = args[0]
		}

		to, err := cmd.Flags().GetString("to")
		utils.HandleError(err)

		if alias == "" || to == "" {
			pterm.Error.Println("You need both an existing alias and a to alias")
		}

		server, _ := cmd.Flags().GetString("server")
		audience, _ := cmd.Flags().GetString("audience")
		err = copy(alias, to, server, audience)
		utils.HandleError(err)

		pterm.Println()
	},
}
View Source
var DeleteCmd = &cobra.Command{
	Use:   "delete",
	Short: "Delete login profile",
	Long: `Delete (an existing) login profile, for example:
mim login delete --alias="local"
`,
	Run: func(cmd *cobra.Command, args []string) {

		pterm.EnableDebugMessages()

		alias, err := cmd.Flags().GetString("alias")
		if alias == "" && len(args) > 0 {
			alias = args[0]
		}

		if alias == "" {
			utils.HandleError(errors.New("an alias must be provided"))
		}

		err = config.Delete(alias)
		utils.HandleError(err)

		pterm.Success.Println("Deleted login alias")
		pterm.Println()
	},
	TraverseChildren: true,
}

addCmd represents the add command

View Source
var ListCmd = &cobra.Command{
	Use:     "list",
	Aliases: []string{"ls"},
	Short:   "Lists all profiles",
	Long: `Lists all configured login profiles. For example:
mim login list
`,
	Run: func(cmd *cobra.Command, args []string) {
		driver := display.ResolveDriver(cmd)
		pterm.EnableDebugMessages()

		alias := viper.GetString("activelogin")

		var err2 error
		if items, err := config.Dump(); err != nil {
			driver.RenderError(err, true)
		} else {
			out := make([][]string, 0)
			out = append(out, []string{"", "Alias", "Server", "Type", "Token", "ClientId", "ClientSecret", "Authorizer", "Audience", "Subject"})
			for k, v := range items {
				data := &config.Config{}
				err2 = json.Unmarshal(v, data)
				if err2 != nil {
					break
				}
				loginType := data.Type
				if data.Type == "" {
					loginType = "client"
				}

				token := data.Token
				if token != "" {
					token = "*****"
					if data.Type == "" {
						loginType = "token"
					}
				}
				secret := data.ClientSecret
				if secret != "" {
					secret = "*****"
				}
				audience := data.Audience
				if audience == "" && data.ClientId != "" {
					audience = data.Server
				}

				active := ""
				if alias == k {
					active = " -> "
				}

				var sub string
				if data.OauthToken != nil && data.OauthToken.AccessToken != "" {
					at, err := jwt.ParseString(data.OauthToken.AccessToken, jwt.WithVerify(false), jwt.WithValidate(false))
					if err != nil {
						token = err.Error()
					} else {
						sub = at.Subject()
					}
				}

				out = append(out, []string{
					active, k, data.Server, loginType, token, data.ClientId, secret, data.Authorizer, audience, sub,
				})
				out = utils.SortOutputList(out, "Alias")
			}
			driver.Render(out, true)
		}
		driver.RenderError(err2, true)
	},
	TraverseChildren: true,
}

ListCmd represents the list command

View Source
var LogoutCmd = &cobra.Command{
	Use:   "logout",
	Short: "Logs out",
	Long: `Logs out from the current profile. To remove the login information you need to delete it instead. For example:
mim logout
`,
	Run: func(cmd *cobra.Command, args []string) {
		driver := display.ResolveDriver(cmd)

		err := removeToken()
		driver.RenderError(err, true)

		UpdateConfig("")

		driver.RenderSuccess("Logged out of profile")
	},
}
View Source
var UseCmd = &cobra.Command{
	Use:   "use",
	Short: "Use a login profile",
	Long: `Uses an already configured login profile. For example:
mim login use local
or
mim login use --alias="dev"
`,
	Run: func(cmd *cobra.Command, args []string) {
		driver := display.ResolveDriver(cmd)

		alias, err := cmd.Flags().GetString("alias")
		driver.RenderError(err, true)

		if alias == "" && len(args) > 0 {
			alias = args[0]
		}

		driver.Msg("")
		driver.RenderSuccess("Setting current login to " + alias)
		_, err = UseLogin(alias)
		driver.RenderError(err, true)
		UpdateConfig(alias)

		pterm.Println()
	},
}

Functions

func AttemptLogin

func AttemptLogin(server string, token string) error

AttemptLogin takes the token and server configuration it is given, and tries to call the /jobs endpoint on the server If it gets a 200 OK, it assumes login is fine, if not, it returns an error

func GetLoginsCompletion

func GetLoginsCompletion(pattern string) []string

func ResolveCredentials

func ResolveCredentials() (string, string, error)

ResolveCredentials is deprecated, and you should realy use the web.ResolveCredentials() instead.

func UpdateConfig

func UpdateConfig(alias string)

func UseLogin

func UseLogin(alias string) (*oauth2.Token, error)

Types

type AuthCode

type AuthCode struct {
	Code     string `json:"code"`
	ClientId string `json:"clientId"`
}

type Response

type Response struct {
	Message string `json:"message"`
}

type UserLogin

type UserLogin struct {
}

func NewUserLogin

func NewUserLogin() *UserLogin

func (*UserLogin) Login

func (l *UserLogin) Login(cfg *config.Config) (*oauth2.Token, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL