...

Source file src/github.com/go-kivik/kivik/v4/x/kivikd/cookies/cookies.go

Documentation: github.com/go-kivik/kivik/v4/x/kivikd/cookies

     1  // Licensed under the Apache License, Version 2.0 (the "License"); you may not
     2  // use this file except in compliance with the License. You may obtain a copy of
     3  // the License at
     4  //
     5  //  http://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     9  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    10  // License for the specific language governing permissions and limitations under
    11  // the License.
    12  
    13  // Package cookies provides cookies utilities.
    14  package cookies
    15  
    16  import (
    17  	"bytes"
    18  	"encoding/base64"
    19  	"fmt"
    20  	"strconv"
    21  )
    22  
    23  // DecodeCookie decodes a Base64-encoded cookie, and returns its component
    24  // parts.
    25  func DecodeCookie(cookie string) (name string, created int64, err error) {
    26  	data, err := base64.RawURLEncoding.DecodeString(cookie)
    27  	if err != nil {
    28  		return "", 0, err
    29  	}
    30  	const partCount = 3
    31  	parts := bytes.SplitN(data, []byte(":"), partCount)
    32  	t, err := strconv.ParseInt(string(parts[1]), 16, 64)
    33  	if err != nil {
    34  		return "", 0, fmt.Errorf("invalid timestamp: %w", err)
    35  	}
    36  	return string(parts[0]), t, nil
    37  }
    38  

View as plain text