feat: add assessment, privacy and support flows
This commit is contained in:
1 parent
ee3738ee73
commit
e4f24fa202
339 files changed
+56454
-1126
No files matched your search
@@ -10,10 +10,11 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
// productPolicySchemaVersion is the layout of the stored document. A new
|
||||
// layout adds a decoder for its version; stored versions are never
|
||||
// rewritten.
|
||||
productPolicySchemaVersion = 1
|
||||
// productPolicySchemaVersionV1 is the original immutable policy layout.
|
||||
productPolicySchemaVersionV1 = 1
|
||||
// productPolicySchemaVersionV2 adds optional policy extensions. Stored
|
||||
// versions are never rewritten when a newer layout is introduced.
|
||||
productPolicySchemaVersionV2 = 2
|
||||
// maxPolicyDocumentBytes bounds a document before decoding. The current
|
||||
// layout encodes to well under 2 KiB.
|
||||
maxPolicyDocumentBytes = 64 << 10
|
||||
@@ -22,9 +23,11 @@ const (
|
||||
// PolicyDocumentHash is the SHA-256 of the canonical policy document.
|
||||
type PolicyDocumentHash [sha256.Size]byte
|
||||
|
||||
// The document types mirror ProductPolicy field by field. Every field is a
|
||||
// pointer, so the decoder can tell a missing field from a zero value: a
|
||||
// missing field is an error, never a default. Field names carry their unit.
|
||||
// The document types mirror ProductPolicy field by field. Required fields are
|
||||
// pointers, so the decoder can tell a missing field from a zero value: a
|
||||
// missing field is an error, never a default. Optional attempt, training, and
|
||||
// support sections are absent only in immutable legacy policy documents.
|
||||
// Field names carry their unit.
|
||||
type productPolicyDocument struct {
|
||||
SchemaVersion *int32 `json:"schema_version"`
|
||||
Invitations *invitationPolicyDocument `json:"invitations"`
|
||||
@@ -32,6 +35,18 @@ type productPolicyDocument struct {
|
||||
Assessment *assessmentPolicyDocument `json:"assessment"`
|
||||
Identity *identityPolicyDocument `json:"identity"`
|
||||
SearchStatus *searchStatusDocument `json:"search_status"`
|
||||
Support *supportPolicyDocument `json:"support,omitempty"`
|
||||
}
|
||||
|
||||
// productPolicyV1Document deliberately excludes extension keys. Its strict
|
||||
// decoder keeps old documents unambiguous even when a newer field is null.
|
||||
type productPolicyV1Document struct {
|
||||
SchemaVersion *int32 `json:"schema_version"`
|
||||
Invitations *invitationPolicyDocument `json:"invitations"`
|
||||
OrganizationQuotas *organizationQuotaDocument `json:"organization_quotas"`
|
||||
Assessment *assessmentPolicyV1Document `json:"assessment"`
|
||||
Identity *identityPolicyDocument `json:"identity"`
|
||||
SearchStatus *searchStatusDocument `json:"search_status"`
|
||||
}
|
||||
|
||||
type invitationPolicyDocument struct {
|
||||
@@ -62,6 +77,20 @@ type organizationTierQuotaDocument struct {
|
||||
}
|
||||
|
||||
type assessmentPolicyDocument struct {
|
||||
Calibration *AssessmentCalibration `json:"calibration"`
|
||||
SameGradeRetryAfterDays *CalendarDays `json:"same_grade_retry_after_days"`
|
||||
MaxAttemptsPerWindow *int32 `json:"max_attempts_per_window"`
|
||||
AttemptWindowDays *CalendarDays `json:"attempt_window_days"`
|
||||
PromotionMinIntervalDays *CalendarDays `json:"promotion_min_interval_days"`
|
||||
ConfirmationThresholdBP *BasisPoints `json:"confirmation_threshold_bp"`
|
||||
HighStrengthThresholdBP *BasisPoints `json:"high_strength_threshold_bp"`
|
||||
PracticeRequired *bool `json:"practice_required"`
|
||||
StopRule *assessmentStopDocument `json:"stop_rule"`
|
||||
Attempt *assessmentAttemptDocument `json:"attempt,omitempty"`
|
||||
Training *assessmentTrainingDocument `json:"training,omitempty"`
|
||||
}
|
||||
|
||||
type assessmentPolicyV1Document struct {
|
||||
Calibration *AssessmentCalibration `json:"calibration"`
|
||||
SameGradeRetryAfterDays *CalendarDays `json:"same_grade_retry_after_days"`
|
||||
MaxAttemptsPerWindow *int32 `json:"max_attempts_per_window"`
|
||||
@@ -73,6 +102,22 @@ type assessmentPolicyDocument struct {
|
||||
StopRule *assessmentStopDocument `json:"stop_rule"`
|
||||
}
|
||||
|
||||
type assessmentAttemptDocument struct {
|
||||
LifetimeHours *Hours `json:"lifetime_hours"`
|
||||
ChainDeadlineHours *Hours `json:"chain_deadline_hours"`
|
||||
MaxSessions *int32 `json:"max_sessions"`
|
||||
IdleTimeoutSeconds *int32 `json:"idle_timeout_seconds"`
|
||||
RealMode *AssessmentRealMode `json:"real_mode"`
|
||||
BasicPassMarkBP *BasisPoints `json:"basic_pass_mark_bp"`
|
||||
}
|
||||
|
||||
type assessmentTrainingDocument struct {
|
||||
DemoExpressDurationSeconds *int32 `json:"demo_express_duration_seconds"`
|
||||
DemoExpressItemCount *int32 `json:"demo_express_item_count"`
|
||||
MaxSessions *int32 `json:"max_sessions"`
|
||||
IdleTimeoutSeconds *int32 `json:"idle_timeout_seconds"`
|
||||
}
|
||||
|
||||
type assessmentStopDocument struct {
|
||||
MinScoredItems *int32 `json:"min_scored_items"`
|
||||
MaxScoredItems *int32 `json:"max_scored_items"`
|
||||
@@ -101,8 +146,20 @@ func encodeProductPolicy(policy ProductPolicy) ([]byte, PolicyDocumentHash, erro
|
||||
quotas := policy.OrganizationQuotas
|
||||
assessment := policy.Assessment
|
||||
identity := policy.Identity
|
||||
var support *supportPolicyDocument
|
||||
if policy.Support != nil {
|
||||
var err error
|
||||
support, err = encodeSupportPolicy(*policy.Support)
|
||||
if err != nil {
|
||||
return nil, PolicyDocumentHash{}, err
|
||||
}
|
||||
}
|
||||
schemaVersion := int32(productPolicySchemaVersionV1)
|
||||
if policy.Assessment.Attempt != nil || policy.Assessment.Training != nil || policy.Support != nil {
|
||||
schemaVersion = productPolicySchemaVersionV2
|
||||
}
|
||||
document := productPolicyDocument{
|
||||
SchemaVersion: pointer(int32(productPolicySchemaVersion)),
|
||||
SchemaVersion: pointer(schemaVersion),
|
||||
Invitations: &invitationPolicyDocument{
|
||||
ResponseDeadlineOptionsDays: pointer(invitations.ResponseDeadlineOptions),
|
||||
DefaultResponseDeadlineDays: pointer(invitations.DefaultResponseDeadline),
|
||||
@@ -137,6 +194,8 @@ func encodeProductPolicy(policy ProductPolicy) ([]byte, PolicyDocumentHash, erro
|
||||
LowerProbabilityBP: pointer(assessment.StopRule.LowerProbability),
|
||||
MaxStandardErrorBP: pointer(assessment.StopRule.MaxStandardError),
|
||||
},
|
||||
Attempt: encodeAssessmentAttemptPolicy(assessment.Attempt),
|
||||
Training: encodeAssessmentTrainingPolicy(assessment.Training),
|
||||
},
|
||||
Identity: &identityPolicyDocument{
|
||||
VerificationTokenLifetimeHours: pointer(identity.VerificationTokenLifetime),
|
||||
@@ -147,6 +206,7 @@ func encodeProductPolicy(policy ProductPolicy) ([]byte, PolicyDocumentHash, erro
|
||||
SearchStatus: &searchStatusDocument{
|
||||
SearchingConfirmationIntervalDays: pointer(policy.SearchStatus.SearchingConfirmationInterval),
|
||||
},
|
||||
Support: support,
|
||||
}
|
||||
encoded, err := json.Marshal(document)
|
||||
if err != nil {
|
||||
@@ -155,6 +215,32 @@ func encodeProductPolicy(policy ProductPolicy) ([]byte, PolicyDocumentHash, erro
|
||||
return encoded, sha256.Sum256(encoded), nil
|
||||
}
|
||||
|
||||
func encodeAssessmentAttemptPolicy(policy *AssessmentAttemptPolicy) *assessmentAttemptDocument {
|
||||
if policy == nil {
|
||||
return nil
|
||||
}
|
||||
return &assessmentAttemptDocument{
|
||||
LifetimeHours: pointer(policy.Lifetime),
|
||||
ChainDeadlineHours: pointer(policy.ChainDeadline),
|
||||
MaxSessions: pointer(policy.MaxSessions),
|
||||
IdleTimeoutSeconds: pointer(policy.IdleTimeoutSeconds),
|
||||
RealMode: pointer(policy.RealMode),
|
||||
BasicPassMarkBP: pointer(policy.BasicPassMark),
|
||||
}
|
||||
}
|
||||
|
||||
func encodeAssessmentTrainingPolicy(policy *AssessmentTrainingPolicy) *assessmentTrainingDocument {
|
||||
if policy == nil {
|
||||
return nil
|
||||
}
|
||||
return &assessmentTrainingDocument{
|
||||
DemoExpressDurationSeconds: pointer(policy.DemoExpressDurationSeconds),
|
||||
DemoExpressItemCount: pointer(policy.DemoExpressItemCount),
|
||||
MaxSessions: pointer(policy.MaxSessions),
|
||||
IdleTimeoutSeconds: pointer(policy.IdleTimeoutSeconds),
|
||||
}
|
||||
}
|
||||
|
||||
func encodeTierQuota(quota OrganizationTierQuota) *organizationTierQuotaDocument {
|
||||
return &organizationTierQuotaDocument{
|
||||
Active: pointer(quota.Active),
|
||||
@@ -163,30 +249,98 @@ func encodeTierQuota(quota OrganizationTierQuota) *organizationTierQuotaDocument
|
||||
}
|
||||
}
|
||||
|
||||
// decodeProductPolicy reads a document strictly: it rejects unknown fields,
|
||||
// missing fields, null values, values of the wrong type or out of the int32
|
||||
// range, an unknown schema version, and trailing data. It checks structure
|
||||
// only; ProductPolicy.Validate checks the values. encoding/json matches field
|
||||
// names without regard to letter case and keeps the last of duplicate keys;
|
||||
// the document hash, computed over the canonical re-encoding, is what pins
|
||||
// the content.
|
||||
// decodeProductPolicy reads a versioned document strictly. It rejects unknown
|
||||
// fields, missing fields, null required values, values of the wrong type or
|
||||
// out of the int32 range, unknown schema versions, and trailing data. It
|
||||
// checks structure only; ProductPolicy.Validate checks the values.
|
||||
func decodeProductPolicy(document []byte) (ProductPolicy, error) {
|
||||
const location = "domain.decodeProductPolicy"
|
||||
if len(document) > maxPolicyDocumentBytes {
|
||||
return ProductPolicy{}, invalidPolicy(location, fmt.Sprintf("document must have at most %d bytes", maxPolicyDocumentBytes))
|
||||
}
|
||||
decoder := json.NewDecoder(bytes.NewReader(document))
|
||||
decoder.DisallowUnknownFields()
|
||||
version, err := decodeProductPolicySchemaVersion(document)
|
||||
if err != nil {
|
||||
return ProductPolicy{}, err
|
||||
}
|
||||
var parsed productPolicyDocument
|
||||
if err := decoder.Decode(&parsed); err != nil {
|
||||
return ProductPolicy{}, fmt.Errorf("%s - decode document: %w: %w", location, ErrInvalidPolicy, err)
|
||||
switch version {
|
||||
case productPolicySchemaVersionV1:
|
||||
var legacy productPolicyV1Document
|
||||
if err := decodeStrictPolicyDocument(document, &legacy); err != nil {
|
||||
return ProductPolicy{}, err
|
||||
}
|
||||
parsed = productPolicyDocument{
|
||||
SchemaVersion: legacy.SchemaVersion,
|
||||
Invitations: legacy.Invitations,
|
||||
OrganizationQuotas: legacy.OrganizationQuotas,
|
||||
Identity: legacy.Identity,
|
||||
SearchStatus: legacy.SearchStatus,
|
||||
}
|
||||
if legacy.Assessment != nil {
|
||||
assessment := legacy.Assessment
|
||||
parsed.Assessment = &assessmentPolicyDocument{
|
||||
Calibration: assessment.Calibration,
|
||||
SameGradeRetryAfterDays: assessment.SameGradeRetryAfterDays,
|
||||
MaxAttemptsPerWindow: assessment.MaxAttemptsPerWindow,
|
||||
AttemptWindowDays: assessment.AttemptWindowDays,
|
||||
PromotionMinIntervalDays: assessment.PromotionMinIntervalDays,
|
||||
ConfirmationThresholdBP: assessment.ConfirmationThresholdBP,
|
||||
HighStrengthThresholdBP: assessment.HighStrengthThresholdBP,
|
||||
PracticeRequired: assessment.PracticeRequired,
|
||||
StopRule: assessment.StopRule,
|
||||
}
|
||||
}
|
||||
case productPolicySchemaVersionV2:
|
||||
if err := decodeStrictPolicyDocument(document, &parsed); err != nil {
|
||||
return ProductPolicy{}, err
|
||||
}
|
||||
assessment := parsed.Assessment
|
||||
hasAssessmentExtension := assessment != nil && (assessment.Attempt != nil || assessment.Training != nil)
|
||||
if !hasAssessmentExtension && parsed.Support == nil {
|
||||
return ProductPolicy{}, invalidPolicy(location, "schema_version 2 requires at least one policy extension")
|
||||
}
|
||||
default:
|
||||
return ProductPolicy{}, invalidPolicy(location, fmt.Sprintf("schema_version must be %d or %d", productPolicySchemaVersionV1, productPolicySchemaVersionV2))
|
||||
}
|
||||
return decodeProductPolicyFields(parsed, version)
|
||||
}
|
||||
|
||||
func decodeProductPolicySchemaVersion(document []byte) (int32, error) {
|
||||
const location = "domain.decodeProductPolicy"
|
||||
var envelope struct {
|
||||
SchemaVersion *int32 `json:"schema_version"`
|
||||
}
|
||||
decoder := json.NewDecoder(bytes.NewReader(document))
|
||||
if err := decoder.Decode(&envelope); err != nil {
|
||||
return 0, fmt.Errorf("%s - decode document: %w: %w", location, ErrInvalidPolicy, err)
|
||||
}
|
||||
if _, err := decoder.Token(); !errors.Is(err, io.EOF) {
|
||||
return ProductPolicy{}, invalidPolicy(location, "document must contain one JSON object")
|
||||
return 0, invalidPolicy(location, "document must contain one JSON object")
|
||||
}
|
||||
if envelope.SchemaVersion == nil {
|
||||
return 0, invalidPolicy(location, "schema_version is required and must be an integer")
|
||||
}
|
||||
return *envelope.SchemaVersion, nil
|
||||
}
|
||||
|
||||
func decodeStrictPolicyDocument[T any](document []byte, parsed *T) error {
|
||||
const location = "domain.decodeProductPolicy"
|
||||
decoder := json.NewDecoder(bytes.NewReader(document))
|
||||
decoder.DisallowUnknownFields()
|
||||
if err := decoder.Decode(parsed); err != nil {
|
||||
return fmt.Errorf("%s - decode document: %w: %w", location, ErrInvalidPolicy, err)
|
||||
}
|
||||
if _, err := decoder.Token(); !errors.Is(err, io.EOF) {
|
||||
return invalidPolicy(location, "document must contain one JSON object")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func decodeProductPolicyFields(parsed productPolicyDocument, schemaVersion int32) (ProductPolicy, error) {
|
||||
const location = "domain.decodeProductPolicy"
|
||||
reader := documentReader{}
|
||||
if version := require(&reader, "schema_version", parsed.SchemaVersion); reader.missing == "" && version != productPolicySchemaVersion {
|
||||
return ProductPolicy{}, invalidPolicy(location, fmt.Sprintf("schema_version must be %d", productPolicySchemaVersion))
|
||||
if version := require(&reader, "schema_version", parsed.SchemaVersion); reader.missing == "" && version != schemaVersion {
|
||||
return ProductPolicy{}, invalidPolicy(location, fmt.Sprintf("schema_version must be %d", schemaVersion))
|
||||
}
|
||||
invitations := require(&reader, "invitations", parsed.Invitations)
|
||||
quotas := require(&reader, "organization_quotas", parsed.OrganizationQuotas)
|
||||
@@ -195,6 +349,28 @@ func decodeProductPolicy(document []byte) (ProductPolicy, error) {
|
||||
stopRule := require(&reader, "assessment.stop_rule", assessment.StopRule)
|
||||
identity := require(&reader, "identity", parsed.Identity)
|
||||
searchStatus := require(&reader, "search_status", parsed.SearchStatus)
|
||||
var attemptPolicy *AssessmentAttemptPolicy
|
||||
if assessment.Attempt != nil {
|
||||
attempt := assessment.Attempt
|
||||
attemptPolicy = &AssessmentAttemptPolicy{
|
||||
Lifetime: require(&reader, "assessment.attempt.lifetime_hours", attempt.LifetimeHours),
|
||||
ChainDeadline: require(&reader, "assessment.attempt.chain_deadline_hours", attempt.ChainDeadlineHours),
|
||||
MaxSessions: require(&reader, "assessment.attempt.max_sessions", attempt.MaxSessions),
|
||||
IdleTimeoutSeconds: require(&reader, "assessment.attempt.idle_timeout_seconds", attempt.IdleTimeoutSeconds),
|
||||
RealMode: require(&reader, "assessment.attempt.real_mode", attempt.RealMode),
|
||||
BasicPassMark: require(&reader, "assessment.attempt.basic_pass_mark_bp", attempt.BasicPassMarkBP),
|
||||
}
|
||||
}
|
||||
var trainingPolicy *AssessmentTrainingPolicy
|
||||
if assessment.Training != nil {
|
||||
training := assessment.Training
|
||||
trainingPolicy = &AssessmentTrainingPolicy{
|
||||
DemoExpressDurationSeconds: require(&reader, "assessment.training.demo_express_duration_seconds", training.DemoExpressDurationSeconds),
|
||||
DemoExpressItemCount: require(&reader, "assessment.training.demo_express_item_count", training.DemoExpressItemCount),
|
||||
MaxSessions: require(&reader, "assessment.training.max_sessions", training.MaxSessions),
|
||||
IdleTimeoutSeconds: require(&reader, "assessment.training.idle_timeout_seconds", training.IdleTimeoutSeconds),
|
||||
}
|
||||
}
|
||||
policy := ProductPolicy{
|
||||
Invitations: InvitationPolicy{
|
||||
ResponseDeadlineOptions: require(&reader, "invitations.response_deadline_options_days", invitations.ResponseDeadlineOptionsDays),
|
||||
@@ -228,6 +404,8 @@ func decodeProductPolicy(document []byte) (ProductPolicy, error) {
|
||||
LowerProbability: require(&reader, "assessment.stop_rule.lower_probability_bp", stopRule.LowerProbabilityBP),
|
||||
MaxStandardError: require(&reader, "assessment.stop_rule.max_standard_error_bp", stopRule.MaxStandardErrorBP),
|
||||
},
|
||||
Attempt: attemptPolicy,
|
||||
Training: trainingPolicy,
|
||||
},
|
||||
Identity: IdentityPolicy{
|
||||
VerificationTokenLifetime: require(&reader, "identity.verification_token_lifetime_hours", identity.VerificationTokenLifetimeHours),
|
||||
@@ -238,6 +416,7 @@ func decodeProductPolicy(document []byte) (ProductPolicy, error) {
|
||||
SearchStatus: SearchStatusPolicy{
|
||||
SearchingConfirmationInterval: require(&reader, "search_status.searching_confirmation_interval_days", searchStatus.SearchingConfirmationIntervalDays),
|
||||
},
|
||||
Support: decodeOptionalSupportPolicy(&reader, parsed.Support),
|
||||
}
|
||||
if reader.missing != "" {
|
||||
return ProductPolicy{}, invalidPolicy(location, reader.missing+" is required")
|
||||
|
||||
Reference in new issue
Block a user