<commit_before>package sa

import (
	"encoding/json"
	"fmt"
	"math"
	"net"
	"time"

	"github.com/letsencrypt/boulder/core"
	"github.com/letsencrypt/boulder/probs"
	jose "github.com/square/go-jose"
)

var mediumBlobSize = int(math.Pow(2, 24))

type issuedNameModel struct {
	ID           int64     `db:"id"`
	ReversedName string    `db:"reversedName"`
	NotBefore    time.Time `db:"notBefore"`
	Serial       string    `db:"serial"`
}

// regModel is the description of a core.Registration in the database.
type regModel struct {
	ID        int64    `db:"id"`
	Key       []byte   `db:"jwk"`
	KeySHA256 string   `db:"jwk_sha256"`
	Contact   []string `db:"contact"`
	Agreement string   `db:"agreement"`
	// InitialIP is stored as sixteen binary bytes, regardless of whether it
	// represents a v4 or v6 IP address.
	InitialIP []byte    `db:"initialIp"`
	CreatedAt time.Time `db:"createdAt"`
	Status    string    `db:"status"`
	LockCol   int64
}

// challModel is the description of a core.Challenge in the database
//
// The Validation field is a stub; the column is only there for backward compatibility.
type challModel struct {
	ID              int64  `db:"id"`
	AuthorizationID string `db:"authorizationID"`

	Type   string          `db:"type"`
	Status core.AcmeStatus `db:"status"`
	Error  []byte          `db:"error"`
	// This field is unused, but is kept temporarily to avoid a database migration.
	// TODO(#1818): remove
	Validated        *time.Time `db:"validated"`
	Token            string     `db:"token"`
	KeyAuthorization string     `db:"keyAuthorization"`
	ValidationRecord []byte     `db:"validationRecord"`

	LockCol int64

	// obsoleteTLS is obsoleted. Only used for simpleHTTP and simpleHTTP is
	// dead. Only still here because gorp complains if its gone and locks up if
	// its private.
	ObsoleteTLS *bool `db:"tls"`
}

// getChallengesQuery fetches exactly the fields in challModel from the
// challenges table.
const getChallengesQuery = `
	SELECT id, authorizationID, type, status, error, validated, token,
		keyAuthorization, validationRecord, tls
	FROM challenges WHERE authorizationID = :authID ORDER BY id ASC`

// newReg creates a reg model object from a core.Registration
func registrationToModel(r *core.Registration) (*regModel, error) {
	key, err := json.Marshal(r.Key)
	if err != nil {
		return nil, err
	}

	sha, err := core.KeyDigest(r.Key)
	if err != nil {
		return nil, err
	}<commit_msg>Remove obsolete TLS field from challenge model. (#2166)<commit_after>package sa

import (
	"encoding/json"
	"fmt"
	"math"
	"net"
	"time"

	"github.com/letsencrypt/boulder/core"
	"github.com/letsencrypt/boulder/probs"
	jose "github.com/square/go-jose"
)

var mediumBlobSize = int(math.Pow(2, 24))

type issuedNameModel struct {
	ID           int64     `db:"id"`
	ReversedName string    `db:"reversedName"`
	NotBefore    time.Time `db:"notBefore"`
	Serial       string    `db:"serial"`
}

// regModel is the description of a core.Registration in the database.
type regModel struct {
	ID        int64    `db:"id"`
	Key       []byte   `db:"jwk"`
	KeySHA256 string   `db:"jwk_sha256"`
	Contact   []string `db:"contact"`
	Agreement string   `db:"agreement"`
	// InitialIP is stored as sixteen binary bytes, regardless of whether it
	// represents a v4 or v6 IP address.
	InitialIP []byte    `db:"initialIp"`
	CreatedAt time.Time `db:"createdAt"`
	Status    string    `db:"status"`
	LockCol   int64
}

// challModel is the description of a core.Challenge in the database
//
// The Validation field is a stub; the column is only there for backward compatibility.
type challModel struct {
	ID              int64  `db:"id"`
	AuthorizationID string `db:"authorizationID"`

	Type   string          `db:"type"`
	Status core.AcmeStatus `db:"status"`
	Error  []byte          `db:"error"`
	// This field is unused, but is kept temporarily to avoid a database migration.
	// TODO(#1818): remove
	Validated        *time.Time `db:"validated"`
	Token            string     `db:"token"`
	KeyAuthorization string     `db:"keyAuthorization"`
	ValidationRecord []byte     `db:"validationRecord"`

	LockCol int64
}

// getChallengesQuery fetches exactly the fields in challModel from the
// challenges table.
const getChallengesQuery = `
	SELECT id, authorizationID, type, status, error, validated, token,
		keyAuthorization, validationRecord
	FROM challenges WHERE authorizationID = :authID ORDER BY id ASC`

// newReg creates a reg model object from a core.Registration
func registrationToModel(r *core.Registration) (*regModel, error) {
	key, err := json.Marshal(r.Key)
	if err != nil {
		return nil, err
	}

	sha, err := core.KeyDigest(r.Key)
	if err != nil {
		return nil, err
	}
{'fraction_non_alphanumeric': 0.11113485787561445, 'fraction_numerical': 0.0115409275486215, 'mean_word_length': 3.4656488549618323, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3566086', 'n_tokens_mistral': 1535, 'n_tokens_neox': 1454, 'n_words': 483}