Skip to content

Commit

Permalink
Merge pull request #11 from StackPointCloud/two_factor_auth
Browse files Browse the repository at this point in the history
Two factor auth
  • Loading branch information
Jasmin Gacic authored Oct 8, 2018
2 parents b511c90 + 7a5913c commit 8f03d88
Show file tree
Hide file tree
Showing 19 changed files with 879 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 72 additions & 0 deletions cmd/disable2fa.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright © 2018 Jasmin Gacic <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

// disable2faCmd represents the disable2fa command
var disable2faCmd = &cobra.Command{
Use: "disable",
Short: "Disables two factor authentication",
Long: `Example:
Disable two factor authentication via SMS
packet 2fa disable -s -t [token]
Disable two factor authentication via APP
packet 2fa disable -a -t [token]
`,
Run: func(cmd *cobra.Command, args []string) {
if sms == false && app == false {
fmt.Println("Either sms or app should be set")
return
} else if sms == true && app == true {
fmt.Println("Either sms or app can be set.")
return
} else if sms {
_, err := PacknGo.TwoFactorAuth.DisableSms(token)
if err != nil {
fmt.Println("Client error:", err)
return
}
fmt.Println("Two factor authentication successfuly disabled.")
} else if app {
_, err := PacknGo.TwoFactorAuth.DisableApp(token)
if err != nil {
fmt.Println("Client error:", err)
return
}
fmt.Println("Two factor authentication successfuly disabled.")
}
},
}

func init() {
twofaCmd.AddCommand(disable2faCmd)
disable2faCmd.Flags().BoolVarP(&sms, "sms", "s", false, "Issues SMS otp token to user's phone")
disable2faCmd.Flags().BoolVarP(&app, "app", "a", false, "Issues otp uri for auth application")
disable2faCmd.Flags().StringVarP(&token, "token", "t", "", "Two factor authentication token")
disable2faCmd.MarkFlagRequired("token")
}
74 changes: 74 additions & 0 deletions cmd/enable2fa.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright © 2018 Jasmin Gacic <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

var token string

// enable2faCmd represents the enable2fa command
var enable2faCmd = &cobra.Command{
Use: "enable",
Short: "Enables two factor authentication",
Long: `Example:
Enable two factor authentication via SMS
packet 2fa enable -s -t [token]
Enable two factor authentication via APP
packet 2fa enable -a -t [token]
`,
Run: func(cmd *cobra.Command, args []string) {
if sms == false && app == false {
fmt.Println("Either sms or app should be set")
return
} else if sms == true && app == true {
fmt.Println("Either sms or app can be set.")
return
} else if sms {
_, err := PacknGo.TwoFactorAuth.EnableSms(token)
if err != nil {
fmt.Println("Client error:", err)
return
}
fmt.Println("Two factor authentication successfuly enabled.")
} else if app {
_, err := PacknGo.TwoFactorAuth.EnableApp(token)
if err != nil {
fmt.Println("Client error:", err)
return
}
fmt.Println("Two factor authentication successfuly enabled.")
}
},
}

func init() {
twofaCmd.AddCommand(enable2faCmd)
enable2faCmd.Flags().BoolVarP(&sms, "sms", "s", false, "Issues SMS otp token to user's phone")
enable2faCmd.Flags().BoolVarP(&app, "app", "a", false, "Issues otp uri for auth application")
enable2faCmd.Flags().StringVarP(&token, "token", "t", "", "Two factor authentication token")
enable2faCmd.MarkFlagRequired("token")
}
36 changes: 36 additions & 0 deletions cmd/hardware_reservations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright © 2018 Jasmin Gacic <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package cmd

import (
"github.com/spf13/cobra"
)

// hardwareReservationsCmd represents the hardwareReservations command
var hardwareReservationsCmd = &cobra.Command{
Use: "hardware_reservation",
Short: "Hardware reservation operations",
Long: `Hardware reservation operations: get, move`,
}

func init() {
rootCmd.AddCommand(hardwareReservationsCmd)
}
59 changes: 59 additions & 0 deletions cmd/move_hardware_reservation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright © 2018 Jasmin Gacic <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

// moveHardwareReservationCmd represents the moveHardwareReservation command
var moveHardwareReservationCmd = &cobra.Command{
Use: "move",
Short: "Move hardware reservation to another project",
Long: `Example:
packet hardware_reservation move -i [hardware_reservation_UUID] -p [project_UUID]
`,
Run: func(cmd *cobra.Command, args []string) {
header := []string{"ID", "Facility", "Plan", "Created"}
r, _, err := PacknGo.HardwareReservations.Move(hardwareReservationID, projectID)
if err != nil {
fmt.Println("Client error:", err)
return
}

data := make([][]string, 1)

data[0] = []string{r.ID, r.Facility.Code, r.Plan.Name, r.CreatedAt.String()}

output(r, header, &data)
},
}

func init() {
hardwareReservationsCmd.AddCommand(moveHardwareReservationCmd)
moveHardwareReservationCmd.Flags().StringVarP(&hardwareReservationID, "id", "i", "", "UUID of the hardware reservation")
moveHardwareReservationCmd.Flags().StringVarP(&projectID, "project-id", "p", "", "UUID of the project")
moveHardwareReservationCmd.MarkFlagRequired("project-id")
moveHardwareReservationCmd.MarkFlagRequired("id")
}
79 changes: 79 additions & 0 deletions cmd/receive_2fa.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright © 2018 Jasmin Gacic <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

var app bool
var sms bool

// receive2faCmd represents the receive2fa command
var receive2faCmd = &cobra.Command{
Use: "receive",
Short: "Receive two factor authentication token",
Long: `Example:
Issue the token via SMS:
packet 2fa receive -s
Issue the token via app:
packet 2fa receive -a
`,
Run: func(cmd *cobra.Command, args []string) {
if sms == false && app == false {
fmt.Println("Either sms or app should be set")
return
} else if sms == true && app == true {
fmt.Println("Either sms or app can be set.")
return
} else if sms {
_, err := PacknGo.TwoFactorAuth.ReceiveSms()
if err != nil {
fmt.Println("Client error:", err)
return
}

fmt.Println("SMS token sent to your phone")
} else if app {
otpURI, _, err := PacknGo.TwoFactorAuth.SeedApp()
if err != nil {
fmt.Println("Client error:", err)
return
}

data := make([][]string, 1)

data[0] = []string{otpURI}
header := []string{"OTP URI"}
output(otpURI, header, &data)
}
},
}

func init() {
twofaCmd.AddCommand(receive2faCmd)
receive2faCmd.Flags().BoolVarP(&sms, "sms", "s", false, "Issues SMS otp token to user's phone")
receive2faCmd.Flags().BoolVarP(&app, "app", "a", false, "Issues otp uri for auth application")
}
Loading

0 comments on commit 8f03d88

Please sign in to comment.