Skip to content

Commit

Permalink
update saml_assertation api to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
wardviaene committed Feb 22, 2023
1 parent de7730a commit 13c3e14
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 38 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
config.yaml
/onelogin-auth-*
.DS_Store
8 changes: 4 additions & 4 deletions cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,17 @@ var loginCmd = &cobra.Command{

//MFA Device verification
var deviceID *int
if assertionResponse.Status.Message == "MFA is required for this user" {
if assertionResponse.Message == onelogin.MFA_REQUIRED_STRING {
fmt.Println("MFA Required, select a device:")
deviceID, err = getDeviceID(assertionResponse.Data[0].Devices)
deviceID, err = getDeviceID(assertionResponse.Devices)
if err != nil {
log.Fatalln(err)
}
mfaCode, err := utils.PromptForSecretString("MFA Code")
if err != nil {
log.Fatalln(err)
}
verificationResponse, err := onelogin.VerifyFactor(token, *deviceID, appID, assertionResponse.Data[0].StateToken, mfaCode)
verificationResponse, err := onelogin.VerifyFactor(token, *deviceID, appID, assertionResponse.StateToken, mfaCode)
if err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down Expand Up @@ -166,7 +166,7 @@ func getDeviceID(devices []onelogin.Device) (*int, error) {
}
for _, v := range devices {
if v.DeviceType == selectedDeviceType {
return &v.DeviceId, nil
return &v.DeviceID, nil
}
}
return nil, fmt.Errorf("No device found")
Expand Down
42 changes: 18 additions & 24 deletions internal/onelogin/saml-assertion.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,34 @@ import (
"net/http"
)

const MFA_REQUIRED_STRING = "MFA is required for this user"

type SAMLAssertionBody struct {
UsernameOrEmail string `json:"username_or_email"`
Password string `json:"password"`
AppID string `json:"app_id"`
SubDomain string `json:"subdomain"`
}

type SAMLAssertionResponse struct {
Data []struct {
Devices []Device `json:"devices"`
CallbackUrl string `json:"callback_url"`
User struct {
Username string `json:"username"`
Email string `json:"email"`
Lastname string `json:"lastname"`
Id int `json:"id"`
Firstname string `json:"firstname"`
} `json:"user"`
StateToken string `json:"state_token"`
} `json:"data"`
Status struct {
Message string `json:"message"`
Error bool `json:"error"`
Type string `json:"type"`
Code int `json:"code"`
} `json:"status"`
StateToken string `json:"state_token"`
Message string `json:"message"`
Devices []Device `json:"devices"`
CallbackURL string `json:"callback_url"`
User User `json:"user"`
}

type Device struct {
DeviceId int `json:"device_id"`
DeviceID int `json:"device_id"`
DeviceType string `json:"device_type"`
}
type User struct {
Lastname string `json:"lastname"`
Username string `json:"username"`
Email string `json:"email"`
Firstname string `json:"firstname"`
ID int `json:"id"`
}

const SAMLAssertionURl = OneLoginAPIURL + "api/1/saml_assertion"
const SAMLAssertionURl = OneLoginAPIURL + "api/2/saml_assertion"

func SAMLAssertion(token, login, password, appID, oneloginDomain string) (*SAMLAssertionResponse, error) {

Expand Down Expand Up @@ -75,8 +69,8 @@ func SAMLAssertion(token, login, password, appID, oneloginDomain string) (*SAMLA
if err != nil {
return nil, err
}
if responseObject.Status.Code != 200 {
return nil, fmt.Errorf(responseObject.Status.Message)
if responseObject.Message != "Success" && responseObject.Message != MFA_REQUIRED_STRING {
return nil, fmt.Errorf(responseObject.Message)
}

return &responseObject, nil
Expand Down
15 changes: 5 additions & 10 deletions internal/onelogin/saml-verify-device.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,11 @@ type VerifyFactorBody struct {
}

type VerifyFactorResponse struct {
Data string `json:"data"`
Status struct {
Message string `json:"message"`
Error bool `json:"error"`
Type string `json:"type"`
Code int `json:"code"`
} `json:"status"`
Data string `json:"data"`
Message string `json:"message"`
}

const VerifyFactorURL = OneLoginAPIURL + "api/1/saml_assertion/verify_factor"
const VerifyFactorURL = OneLoginAPIURL + "api/2/saml_assertion/verify_factor"

func VerifyFactor(token string, deviceID int, appID string, stateToken string, mfaCode string) (*VerifyFactorResponse, error) {
newBody := VerifyFactorBody{
Expand Down Expand Up @@ -59,8 +54,8 @@ func VerifyFactor(token string, deviceID int, appID string, stateToken string, m
if err != nil {
return nil, err
}
if responseObject.Status.Code != 200 {
return nil, fmt.Errorf(responseObject.Status.Message)
if responseObject.Message != "Success" {
return nil, fmt.Errorf(responseObject.Message)
}

return &responseObject, nil
Expand Down

0 comments on commit 13c3e14

Please sign in to comment.