Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove ability to launch using the v1 pattern #71

Merged
merged 4 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 2 additions & 88 deletions authentication/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,45 +156,6 @@ func getSurveyMetadataFromClaims(claimValues map[string][]string, data map[strin
claims["survey_metadata"] = surveyMetadata
}

func generateClaims(claimValues map[string][]string, launcherSchema surveys.LauncherSchema) (claims map[string]interface{}) {

var roles []string
if rolesValues, ok := claimValues["roles"]; ok {
roles = rolesValues
} else {
roles = []string{"dumper"}
}

claims = make(map[string]interface{})

claims["roles"] = roles
TxID, _ := uuid.NewV4()
claims["tx_id"] = TxID.String()

for key, value := range claimValues {
if key != "roles" {
if value[0] != "" {
claims[key] = value[0]
}
} else {
claims[key] = value
}
}
if len(claimValues["form_type"]) > 0 && len(claimValues["eq_id"]) > 0 {
log.Println("Deleting schema name from claims")
delete(claims, "schema_name")
} else {
// When quicklaunching, schema_name will not be set, but launcherSchema will have the schema_name.
if len(claimValues["schema_name"]) == 0 && launcherSchema.Name != "" {
claims["schema_name"] = launcherSchema.Name
}
}

log.Printf("Using claims: %s", claims)

return claims
}

func generateClaimsV2(claimValues map[string][]string, schema QuestionnaireSchema) (claims map[string]interface{}) {

var roles []string
Expand Down Expand Up @@ -414,49 +375,6 @@ func getStringOrDefault(key string, values map[string][]string, defaultValue str
return defaultValue
}

// GenerateTokenFromDefaults coverts a set of DEFAULT values into a JWT
func GenerateTokenFromDefaults(schemaURL string, accountServiceURL string, accountServiceLogOutURL string, urlValues url.Values) (token string, error string) {
launcherSchema, validationError := launcherSchemaFromURL(schemaURL)
if validationError != "" {
return "", validationError
}

claims := make(map[string]interface{})
urlValues["account_service_url"] = []string{accountServiceURL}
urlValues["account_service_log_out_url"] = []string{accountServiceLogOutURL}
claims = generateClaims(urlValues, launcherSchema)

requiredMetadata, error := getRequiredSchemaMetadata(launcherSchema)
if error != "" {
return "", fmt.Sprintf("getRequiredSchemaMetadata failed err: %v", error)
}

for _, metadata := range requiredMetadata {
if metadata.Validator == "boolean" {
claims[metadata.Name] = getBooleanOrDefault(metadata.Name, urlValues, false)
continue
}
claims[metadata.Name] = getStringOrDefault(metadata.Name, urlValues, metadata.Default)
}

jwtClaims := GenerateJwtClaims()
for key, v := range jwtClaims {
claims[key] = v
}

schemaClaims := getSchemaClaims(launcherSchema)
for key, v := range schemaClaims {
claims[key] = v
}

token, tokenError := generateTokenFromClaims(claims)
if tokenError != nil {
return token, fmt.Sprintf("GenerateTokenFromDefaults failed err: %v", tokenError)
}

return token, ""
}

// GenerateTokenFromDefaultsV2 coverts a set of DEFAULT values into a JWT
func GenerateTokenFromDefaultsV2(schemaURL string, accountServiceURL string, urlValues url.Values) (token string, error string) {
launcherSchema, validationError := launcherSchemaFromURL(schemaURL)
Expand Down Expand Up @@ -552,7 +470,7 @@ func TransformSchemaParamsToName(postValues url.Values) string {
}

// GenerateTokenFromPost converts a set of POST values into a JWT
func GenerateTokenFromPost(postValues url.Values, launchVersion2 bool) (string, string) {
func GenerateTokenFromPost(postValues url.Values) (string, string) {
log.Println("POST received: ", postValues)

schemaName := TransformSchemaParamsToName(postValues)
Expand All @@ -568,11 +486,7 @@ func GenerateTokenFromPost(postValues url.Values, launchVersion2 bool) (string,

claims := make(map[string]interface{})

if launchVersion2 {
claims = generateClaimsV2(postValues, schema)
} else {
claims = generateClaims(postValues, launcherSchema)
}
claims = generateClaimsV2(postValues, schema)

jwtClaims := GenerateJwtClaims()
for key, v := range jwtClaims {
Expand Down
23 changes: 3 additions & 20 deletions launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,7 @@ func redirectURL(w http.ResponseWriter, r *http.Request) {
token := ""
err := ""

if launchVersion == "v2" {
token, err = authentication.GenerateTokenFromPost(r.PostForm, true)
} else {
token, err = authentication.GenerateTokenFromPost(r.PostForm, false)
}
token, err = authentication.GenerateTokenFromPost(r.PostForm)

if err != "" {
http.Error(w, err, 500)
Expand All @@ -167,20 +163,11 @@ func redirectURL(w http.ResponseWriter, r *http.Request) {
func quickLauncherHandler(w http.ResponseWriter, r *http.Request) {
hostURL := settings.Get("SURVEY_RUNNER_URL")
accountServiceURL := getAccountServiceURL(r)
AccountServiceLogOutURL := getAccountServiceURL(r)
urlValues := r.URL.Query()
schemaURL := urlValues.Get("schema_url")
version := urlValues.Get("version")
liamtoozer marked this conversation as resolved.
Show resolved Hide resolved
launchVersion2 := true

defaultValues := authentication.GetDefaultValues()
if version == "" {
urlValues.Add("version", defaultValues["version"])
} else if version == "v1" {
launchVersion2 = false
} else {
urlValues.Add("version", version)
}
urlValues.Add("version", defaultValues["version"])

log.Println("Quick launch request received", schemaURL)

Expand All @@ -195,11 +182,7 @@ func quickLauncherHandler(w http.ResponseWriter, r *http.Request) {
token := ""
err := ""

if launchVersion2 {
token, err = authentication.GenerateTokenFromDefaultsV2(schemaURL, accountServiceURL, urlValues)
} else {
token, err = authentication.GenerateTokenFromDefaults(schemaURL, accountServiceURL, AccountServiceLogOutURL, urlValues)
}
token, err = authentication.GenerateTokenFromDefaultsV2(schemaURL, accountServiceURL, urlValues)

if err != "" {
http.Error(w, err, 400)
Expand Down
30 changes: 7 additions & 23 deletions templates/launch.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ <h1>Launch a survey</h1>
<strong><u>Launch Pattern</u></strong>
<div class="field-container">
<label for="launch_pattern">Version</label>
<select id="launch_pattern" name="version" class="qa-select-version" onchange="loadMetadataForSchemaName(); ">
<option value="v1">v1</option>
<option value="v2" selected="selected">v2</option>
</select>
<input id="launch_pattern" name="version" type="text" value="v2" class="qa-select-version" onload="loadMetadataForSchemaName();">
</div>

<strong><u>Launch by Schema Name</u></strong>
Expand Down Expand Up @@ -269,27 +266,14 @@ <h3>Runner Data</h3>
}

function includeSurveyMetadataFields(schema_name, survey_type) {
let launchPattern = document.querySelector("#launch_pattern").value
let eqIdValue = schema_name.split('_')[0]
let formTypeValue = schema_name.split("_").slice(1).join("_")

if (launchPattern === "v1") {
document.querySelector('#survey_metadata_fields').innerHTML = `<h3>${survey_type} Survey Metadata</h3>
<div class="field-container">
<label for="eq_id">eq_id</label>
<input id="eq_id" name="eq_id" type="text" value="${eqIdValue}" class="qa-eq_id" >
</div>
<div class="field-container">
<label for="form_type">form_type</label>
<input id="form_type" name="form_type" type="text" value="${formTypeValue}" class="qa-form_type">
</div>`
} else {
document.querySelector('#survey_metadata_fields').innerHTML = `<h3>${survey_type} Survey Metadata</h3>
<div class="field-container">
<label for="form_type">form_type</label>
<input id="form_type" name="form_type" type="text" value="${formTypeValue}" class="qa-form_type">
</div>`
}
document.querySelector('#survey_metadata_fields').innerHTML = `<h3>${survey_type} Survey Metadata</h3>
<div class="field-container">
<label for="form_type">form_type</label>
<input id="form_type" name="form_type" type="text" value="${formTypeValue}" class="qa-form_type">
</div>`


showSupplementaryData(true);
document.querySelector('#survey_metadata_fields').classList.remove("supplementary-data--hidden");
Expand Down
Loading