auth

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package auth provides the auth command group for the medic CLI.

Index

Constants

This section is empty.

Variables

View Source
var AuthCmd = &cobra.Command{
	Use:   "auth",
	Short: "Authorize and manage accounts within a mediator control plane",
	Long: `The medic auth command group lets you create accounts and grant or revoke
authorization to existing accounts within a mediator control plane.`,
	RunE: func(cmd *cobra.Command, args []string) error {
		return cmd.Usage()
	},
}

AuthCmd represents the account command

View Source
var Auth_refreshCmd = &cobra.Command{
	Use:   "refresh",
	Short: "Refresh credentials",
	Long:  `It refreshes credentials for one user`,
	PreRun: func(cmd *cobra.Command, args []string) {
		if err := viper.BindPFlags(cmd.Flags()); err != nil {
			fmt.Fprintf(os.Stderr, "Error binding flags: %s\n", err)
		}
	},
	Run: func(cmd *cobra.Command, args []string) {

		oldCreds, err := util.LoadCredentials()
		util.ExitNicelyOnError(err, "Error loading credentials")

		conn, err := util.GrpcForCommand(cmd)
		util.ExitNicelyOnError(err, "Error getting grpc connection")
		defer conn.Close()

		ctx, cancel := context.WithTimeout(context.Background(), time.Second)
		defer cancel()

		client := pb.NewAuthServiceClient(conn)
		util.ExitNicelyOnError(err, "Error getting grpc connection")

		resp, err := client.RefreshToken(ctx, &pb.RefreshTokenRequest{})
		util.ExitNicelyOnError(err, "Error refreshing token")

		creds := util.Credentials{
			AccessToken:           resp.AccessToken,
			RefreshToken:          oldCreds.RefreshToken,
			AccessTokenExpiresIn:  int(resp.AccessTokenExpiresIn),
			RefreshTokenExpiresIn: oldCreds.RefreshTokenExpiresIn,
		}

		filePath, err := util.SaveCredentials(creds)
		util.ExitNicelyOnError(err, "Error saving credentials")

		fmt.Printf("Credentials saved to %s\n", filePath)
	},
}

Auth_refreshCmd represents the auth refresh command

View Source
var Auth_revokeCmd = &cobra.Command{
	Use:   "revoke",
	Short: "Revoke access tokens",
	Long:  `It can revoke access tokens for one user or for all.`,
	PreRun: func(cmd *cobra.Command, args []string) {
		if err := viper.BindPFlags(cmd.Flags()); err != nil {
			fmt.Fprintf(os.Stderr, "Error binding flags: %s\n", err)
		}
	},
	Run: func(cmd *cobra.Command, args []string) {

		all := util.GetConfigValue("all", "all", cmd, false).(bool)
		user := viper.GetInt32("user-id")

		if all && user != 0 {
			fmt.Fprintf(os.Stderr, "Error: you can't use --all and --user-id together\n")
			os.Exit(1)
		}

		if !all && user == 0 {
			fmt.Fprintf(os.Stderr, "Error: you must use either --all or --user-id\n")
			os.Exit(1)
		}

		conn, err := util.GrpcForCommand(cmd)
		util.ExitNicelyOnError(err, "Error getting grpc connection")
		defer conn.Close()

		util.ExitNicelyOnError(err, "Error getting grpc connection")

		ctx, cancel := util.GetAppContext()
		defer cancel()
		client := pb.NewAuthServiceClient(conn)
		if all {
			_, err := client.RevokeTokens(ctx, &pb.RevokeTokensRequest{})
			util.ExitNicelyOnError(err, "Error revoking tokens")
			cmd.Println("Revoked all tokens")
		} else {
			_, err := client.RevokeUserToken(ctx, &pb.RevokeUserTokenRequest{UserId: user})
			util.ExitNicelyOnError(err, "Error revoking tokens")
			cmd.Println("Revoked token for user", user)
		}
	},
}

Auth_revokeCmd represents the auth revoke command

View Source
var Auth_revokeproviderCmd = &cobra.Command{
	Use:   "revoke_provider",
	Short: "Revoke access tokens for provider",
	Long:  `It can revoke access tokens for specific provider.`,
	PreRun: func(cmd *cobra.Command, args []string) {
		if err := viper.BindPFlags(cmd.Flags()); err != nil {
			fmt.Fprintf(os.Stderr, "Error binding flags: %s\n", err)
		}
	},
	Run: func(cmd *cobra.Command, args []string) {

		all := util.GetConfigValue("all", "all", cmd, false).(bool)
		group := viper.GetInt32("group-id")
		provider := util.GetConfigValue("provider", "provider", cmd, "").(string)

		if all && group != 0 {
			fmt.Fprintf(os.Stderr, "Error: you can't use --all and --group-id together\n")
			os.Exit(1)
		}

		conn, err := util.GrpcForCommand(cmd)
		util.ExitNicelyOnError(err, "Error getting grpc connection")
		defer conn.Close()

		ctx, cancel := util.GetAppContext()
		defer cancel()
		client := pb.NewOAuthServiceClient(conn)
		if all {
			result, err := client.RevokeOauthTokens(ctx, &pb.RevokeOauthTokensRequest{Provider: provider})
			util.ExitNicelyOnError(err, "Error revoking tokens")
			cmd.Println("Revoked a total of ", result.RevokedTokens, " tokens")
		} else {
			_, err := client.RevokeOauthGroupToken(ctx, &pb.RevokeOauthGroupTokenRequest{Provider: provider, GroupId: group})
			util.ExitNicelyOnError(err, "Error revoking tokens")
			if group == 0 {
				cmd.Println("Revoked token for default group")
			} else {
				cmd.Println("Revoked token for group ", group)
			}
		}
	},
}

Auth_revokeproviderCmd represents the auth revoke command

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

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