2018-01-16 02:22:22 +05:30
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2024-02-01 16:41:39 +05:30
|
|
|
"ely.by/chrly/internal/security"
|
2018-01-16 02:22:22 +05:30
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var tokenCmd = &cobra.Command{
|
2024-03-05 17:37:54 +05:30
|
|
|
Use: "token scope1 ...",
|
|
|
|
Example: "token profiles sign",
|
|
|
|
Short: "Creates a new token, which allows to interact with Chrly API",
|
|
|
|
ValidArgs: []string{string(security.ProfilesScope), string(security.SignScope)},
|
2024-02-01 16:41:39 +05:30
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2020-04-19 05:01:09 +05:30
|
|
|
container := shouldGetContainer()
|
2024-02-01 16:41:39 +05:30
|
|
|
var auth *security.Jwt
|
2020-04-19 05:01:09 +05:30
|
|
|
err := container.Resolve(&auth)
|
|
|
|
if err != nil {
|
2024-02-01 16:41:39 +05:30
|
|
|
return err
|
2020-04-19 05:01:09 +05:30
|
|
|
}
|
|
|
|
|
2024-03-05 17:37:54 +05:30
|
|
|
scopes := make([]security.Scope, len(args))
|
|
|
|
for i := range args {
|
|
|
|
scopes[i] = security.Scope(args[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
token, err := auth.NewToken(scopes...)
|
2018-02-15 16:50:17 +05:30
|
|
|
if err != nil {
|
2024-02-01 16:41:39 +05:30
|
|
|
return fmt.Errorf("Unable to create a new token. The error is %v\n", err)
|
2018-01-16 02:22:22 +05:30
|
|
|
}
|
|
|
|
|
2024-02-01 16:41:39 +05:30
|
|
|
fmt.Println(token)
|
|
|
|
|
|
|
|
return nil
|
2018-01-16 02:22:22 +05:30
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
RootCmd.AddCommand(tokenCmd)
|
|
|
|
}
|