github.com/searKing/golang/go@v1.2.117/crypto/auth/auth.go (about)

     1  // Copyright 2022 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // https://www.oauth.com/oauth2-servers/client-registration/client-id-secret/
     6  package auth
     7  
     8  import (
     9  	"encoding/base64"
    10  
    11  	"github.com/searKing/golang/go/crypto/rand"
    12  )
    13  
    14  const (
    15  	DefaultSizeClientID      = 16
    16  	DefaultSizeClientKey     = 16
    17  	DefaultSizeClientSecret  = 32
    18  	DefaultSizeAuthorizeCode = 16
    19  	DefaultSizeUUID          = 64
    20  )
    21  
    22  // ClientID is the ID of client
    23  // Here are some examples of client IDs from services that support OAuth 2.0:
    24  // Foursquare: ZYDPLLBWSK3MVQJSIYHB1OR2JXCY0X2C5UJ2QAR2MAAIT5Q
    25  // Github: 6779ef20e75817b79602
    26  // Google: 292085223830.apps.googleusercontent.com
    27  // Instagram: f2a1ed52710d4533bde25be6da03b6e3
    28  // SoundCloud: 269d98e4922fb3895e9ae2108cbb5064
    29  // Windows Live: 00000000400ECB04
    30  func ClientID() string {
    31  	return ClientIDWithSize(DefaultSizeClientID)
    32  }
    33  func ClientIDWithSize(len int) string {
    34  	return rand.String(len)
    35  }
    36  func ClientSecret() string {
    37  	return ClientSecretWithSize(DefaultSizeClientSecret)
    38  }
    39  
    40  func ClientSecretWithSize(len int) string {
    41  	return rand.StringWithCharset(len, rand.CharsetHexadecimalDigits)
    42  }
    43  
    44  func ClientKey() string {
    45  	return ClientKeyWithSize(DefaultSizeClientKey)
    46  }
    47  
    48  var ClientKeyWithSize = ClientSecretWithSize
    49  
    50  // AuthorizeCode generates code base on tokenN
    51  // tokenN can be generated by uuid.NewRandom()
    52  func AuthorizeCode(tokenN ...byte) string {
    53  	var token []byte
    54  	if len(tokenN) == 0 {
    55  		token = rand.Bytes(DefaultSizeAuthorizeCode)
    56  	} else {
    57  		token = tokenN[:]
    58  	}
    59  	return base64.RawURLEncoding.EncodeToString(token)
    60  }
    61  func AuthorizeCodeWithSize(len int) string {
    62  	return base64.RawURLEncoding.EncodeToString(rand.Bytes(len))
    63  }
    64  
    65  func UUID() string {
    66  	return AuthorizeCode()
    67  }