func ToIdentity()

in lib/persona/persona.go [125:244]


func ToIdentity(ctx context.Context, name string, persona *cpb.TestPersona, scope, visaIssuer string) (*ga4gh.Identity, error) {
	if persona.Passport == nil {
		return nil, fmt.Errorf("persona %q has not configured a test identity token", name)
	}
	sub := getStandardClaim(persona, "sub")
	if len(sub) == 0 {
		sub = name
	}

	iss := visaIssuer
	if len(iss) == 0 {
		iss = getStandardClaim(persona, "iss")
		visaIssuer = iss
	}
	firstName := getStandardClaim(persona, "firstName")
	lastName := getStandardClaim(persona, "lastName")
	fullName := getStandardClaim(persona, "name")
	if len(fullName) == 0 && persona.Ui != nil && len(persona.Ui["label"]) > 0 {
		fullName = persona.Ui["label"]
	}
	splitName := strings.Split(fullName, " ")
	if len(splitName) > 0 && len(firstName) == 0 {
		firstName = splitName[0]
	}
	if len(splitName) > 1 && len(lastName) == 0 {
		lastName = splitName[1]
	}
	if len(splitName) == 1 && len(lastName) > 0 {
		fullName = fullName + " " + lastName
	} else if len(splitName) == 0 && len(firstName) > 0 && len(lastName) > 0 {
		fullName = firstName + " " + lastName
	} else if len(splitName) == 0 && len(firstName) > 0 {
		fullName = firstName + " Persona"
	} else if len(splitName) == 0 && len(lastName) > 0 {
		fullName = "Sam " + lastName
	} else if len(splitName) == 0 {
		names := strings.FieldsFunc(name, nameSplit)
		if len(names) > 1 {
			fullName = strings.Join(names, " ")
		} else {
			fullName = name + " Persona"
		}
		if len(firstName) == 0 {
			firstName = names[0]
		}
		if len(lastName) == 0 {
			if len(names) > 1 {
				lastName = names[1]
			} else {
				lastName = "Persona"
			}
		}
	}
	if len(firstName) == 0 || len(lastName) == 0 {
		splitName = strings.Split(fullName, " ")
		if len(firstName) == 0 {
			firstName = splitName[0]
		}
		if len(lastName) == 0 {
			if len(splitName) > 1 {
				lastName = splitName[1]
			} else {
				lastName = "Persona"
			}
		}
	}
	nickname := getStandardClaim(persona, "nickname")
	if nickname == "" {
		nickname = strings.Split(toName(name), " ")[0]
	}

	if len(persona.Passport.ExtraScopes) > 0 {
		scope = scope + " " + persona.Passport.ExtraScopes
	}

	email := getStandardClaim(persona, "email")
	identity := ga4gh.Identity{
		Subject:         sub,
		Email:           email,
		Issuer:          iss,
		Expiry:          time.Now().Add(180 * 24 * time.Hour).Unix(),
		Scope:           scope,
		AuthorizedParty: getStandardClaim(persona, "azp"),
		Username:        name,
		EmailVerified:   strings.ToLower(getStandardClaim(persona, "email_verified")) == "true",
		Name:            toName(fullName),
		Nickname:        nickname,
		GivenName:       toName(firstName),
		FamilyName:      toName(lastName),
		MiddleName:      getStandardClaim(persona, "middle_name"),
		ZoneInfo:        getStandardClaim(persona, "zoneinfo"),
		Locale:          getStandardClaim(persona, "locale"),
		Picture:         getStandardClaim(persona, "picture"),
		Profile:         getStandardClaim(persona, "profile"),
		Patient:         getStandardClaim(persona, "patient"),
		FhirUser:        getStandardClaim(persona, "fhirUser"),
		Extra: map[string]interface{}{
			"tid": sub,
		},
	}

	if email != "" {
		if persona.Passport.Ga4GhAssertions == nil {
			persona.Passport.Ga4GhAssertions = []*cpb.Assertion{}
		}
		assert := &cpb.Assertion{
			Type:             "LinkedIdentities",
			Value:            url.QueryEscape(email) + "," + url.QueryEscape(visaIssuer),
			Source:           visaIssuer,
			By:               "system",
			AssertedDuration: "30d",
			ExpiresDuration:  "30d",
		}
		persona.Passport.Ga4GhAssertions = append(persona.Passport.Ga4GhAssertions, assert)
	}
	if persona.Passport.Ga4GhAssertions == nil || len(persona.Passport.Ga4GhAssertions) == 0 {
		return &identity, nil
	}
	return populatePersonaVisas(ctx, name, visaIssuer, persona.Passport.Ga4GhAssertions, &identity)
}