2023-12-24 10:18:08 +00:00
// This file is part of the happyDomain (R) project.
// Copyright (c) 2020-2024 happyDomain
// Authors: Pierre-Olivier Mercier, et al.
2020-07-17 22:25:34 +00:00
//
2023-12-24 10:18:08 +00:00
// This program is offered under a commercial and under the AGPL license.
// For commercial licensing, contact us at <contact@happydomain.org>.
2020-07-17 22:25:34 +00:00
//
2023-12-24 10:18:08 +00:00
// For AGPL licensing:
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
2020-07-17 22:25:34 +00:00
//
2023-12-24 10:18:08 +00:00
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
2020-07-17 22:25:34 +00:00
//
2023-12-24 10:18:08 +00:00
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
2020-07-17 22:25:34 +00:00
2023-09-07 10:04:55 +00:00
package providers // import "git.happydns.org/happyDomain/providers"
2020-07-17 22:25:34 +00:00
import (
"errors"
"fmt"
"github.com/ovh/go-ovh/ovh"
2023-09-07 09:37:18 +00:00
"git.happydns.org/happyDomain/config"
"git.happydns.org/happyDomain/forms"
"git.happydns.org/happyDomain/model"
2020-07-17 22:25:34 +00:00
)
2020-10-05 08:20:15 +00:00
func settingsForm ( edit bool ) * forms . CustomForm {
srcFields := [ ] * forms . Field {
& forms . Field {
2020-07-17 22:25:34 +00:00
Id : "endpoint" ,
Type : "string" ,
Label : "Endpoint" ,
Default : "ovh-eu" ,
Choices : [ ] string { "ovh-eu" , "ovh-us" , "ovh-ca" , "soyoustart-eu" , "soyoustart-ca" , "kimsufi-eu" , "kimsufi-ca" } ,
Required : true ,
Description : "The endpoint depends on your service's seller (OVH, SoYouStart, Kimsufi) and the datacenter location (eu, us, ca). Choose 'ovh-eu' if unsure." ,
} ,
}
if edit {
2020-10-05 08:20:15 +00:00
srcFields = append ( srcFields , & forms . Field {
2020-07-17 22:25:34 +00:00
Id : "consumerkey" ,
Type : "string" ,
Label : "Consumer Key" ,
Placeholder : "xxxxxxxxxx" ,
Description : "The consumer key allows us to access your domains' settings without knowing your OVH credentials. To generate a new key, remove the content of this field before continue." ,
} )
}
2020-10-10 16:46:28 +00:00
form := forms . GenDefaultSettingsForm ( nil )
2020-07-18 01:35:27 +00:00
form . Fields = srcFields
2023-01-18 17:35:25 +00:00
form . NextButtonText = "common.next"
2020-07-18 01:35:27 +00:00
return form
2020-07-17 22:25:34 +00:00
}
2022-12-22 11:56:24 +00:00
func settingsAskCredentials ( cfg * config . Options , recallid string , session * happydns . Session ) ( * forms . CustomForm , map [ string ] interface { } , error ) {
2021-07-05 15:11:17 +00:00
client , err := ovh . NewClient ( "ovh-eu" , appKey , appSecret , "" )
2020-07-17 22:25:34 +00:00
if err != nil {
2022-12-22 11:56:24 +00:00
return nil , nil , fmt . Errorf ( "Unable to generate Consumer key, as OVH client can't be created: %w" , err )
2020-07-17 22:25:34 +00:00
}
// Generate customer key
2022-12-22 11:56:24 +00:00
ckReq := client . NewCkRequestWithRedirection ( cfg . BuildURL_noescape ( "/providers/new/OVHAPI/2?nsprvid=%s" , recallid ) )
2020-07-17 22:25:34 +00:00
ckReq . AddRecursiveRules ( ovh . ReadWrite , "/domain" )
ckReq . AddRules ( ovh . ReadOnly , "/me" )
response , err := ckReq . Do ( )
if err != nil {
2022-12-22 11:56:24 +00:00
return nil , nil , fmt . Errorf ( "Unable to generate Consumer key; OVH returns: %w" , err )
2020-07-17 22:25:34 +00:00
}
// Return some explanation to the user
2020-10-05 08:20:15 +00:00
return & forms . CustomForm {
2022-12-22 11:56:24 +00:00
BeforeText : "In order allows happyDomain to get and update yours domains, you have to let us access them. To avoid storing your credentials, we will store a unique token that will be associated with your account. For this purpose, you will be redirected to an OVH login screen. The registration will automatically continue" ,
NextButtonText : "Go to OVH" ,
2023-01-18 17:35:25 +00:00
PreviousButtonText : "common.previous" ,
2022-12-22 11:56:24 +00:00
NextButtonLink : response . ValidationURL ,
PreviousButtonState : 0 ,
} , map [ string ] interface { } {
"consumerkey" : response . ConsumerKey ,
} , nil
2020-07-17 22:25:34 +00:00
}
2022-12-22 11:56:24 +00:00
func ( s * OVHAPI ) DisplaySettingsForm ( state int32 , cfg * config . Options , session * happydns . Session , genRecallId forms . GenRecallID ) ( * forms . CustomForm , map [ string ] interface { } , error ) {
2020-07-17 22:25:34 +00:00
switch state {
case 0 :
2022-12-22 11:56:24 +00:00
return settingsForm ( s . ConsumerKey != "" ) , nil , nil
2020-07-17 22:25:34 +00:00
case 1 :
2021-07-05 15:11:17 +00:00
if s . ConsumerKey == "" {
2020-07-17 22:25:34 +00:00
recallid := genRecallId ( )
2021-07-05 15:11:17 +00:00
return settingsAskCredentials ( cfg , recallid , session )
2020-07-17 22:25:34 +00:00
} else {
2022-12-22 11:56:24 +00:00
return nil , nil , forms . DoneForm
2020-07-17 22:25:34 +00:00
}
case 2 :
2022-12-22 11:56:24 +00:00
if s . ConsumerKey == "" {
return nil , nil , errors . New ( "Something wierd has happend, as you were not in a consumer key registration process. Please retry." )
2020-07-17 22:25:34 +00:00
} else {
2022-12-22 11:56:24 +00:00
return nil , nil , forms . DoneForm
2020-07-17 22:25:34 +00:00
}
default :
2022-12-22 11:56:24 +00:00
return nil , nil , forms . CancelForm
2020-07-17 22:25:34 +00:00
}
}