github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/model/oauth/access_code.go (about)

     1  package oauth
     2  
     3  import (
     4  	"github.com/cozy/cozy-stack/model/instance"
     5  	"github.com/cozy/cozy-stack/pkg/consts"
     6  	"github.com/cozy/cozy-stack/pkg/couchdb"
     7  	"github.com/cozy/cozy-stack/pkg/crypto"
     8  )
     9  
    10  // AccessCode is struct used during the OAuth2 flow. It has to be persisted in
    11  // CouchDB, not just sent as a JSON Web Token, because it can be used only
    12  // once (no replay attacks).
    13  type AccessCode struct {
    14  	Code      string `json:"_id,omitempty"`
    15  	CouchRev  string `json:"_rev,omitempty"`
    16  	ClientID  string `json:"client_id"`
    17  	IssuedAt  int64  `json:"issued_at"`
    18  	Scope     string `json:"scope"`
    19  	Challenge string `json:"code_challenge,omitempty"`
    20  }
    21  
    22  // ID returns the access code qualified identifier
    23  func (ac *AccessCode) ID() string { return ac.Code }
    24  
    25  // Rev returns the access code revision
    26  func (ac *AccessCode) Rev() string { return ac.CouchRev }
    27  
    28  // DocType returns the access code document type
    29  func (ac *AccessCode) DocType() string { return consts.OAuthAccessCodes }
    30  
    31  // Clone implements couchdb.Doc
    32  func (ac *AccessCode) Clone() couchdb.Doc { cloned := *ac; return &cloned }
    33  
    34  // SetID changes the access code qualified identifier
    35  func (ac *AccessCode) SetID(id string) { ac.Code = id }
    36  
    37  // SetRev changes the access code revision
    38  func (ac *AccessCode) SetRev(rev string) { ac.CouchRev = rev }
    39  
    40  // CreateAccessCode an access code for the given clientID, persisted in CouchDB
    41  func CreateAccessCode(i *instance.Instance, client *Client, scope, challenge string) (*AccessCode, error) {
    42  	if client.Pending {
    43  		client.Pending = false
    44  		client.ClientID = ""
    45  		_ = couchdb.UpdateDoc(i, client)
    46  		client.ClientID = client.CouchID
    47  	}
    48  
    49  	ac := &AccessCode{
    50  		ClientID:  client.ClientID,
    51  		IssuedAt:  crypto.Timestamp(),
    52  		Scope:     scope,
    53  		Challenge: challenge,
    54  	}
    55  	if err := couchdb.CreateDoc(i, ac); err != nil {
    56  		return nil, err
    57  	}
    58  	return ac, nil
    59  }
    60  
    61  var _ couchdb.Doc = &AccessCode{}