2024-01-04 14:51:35 +00:00
|
|
|
package encoding
|
2024-01-04 13:21:19 +00:00
|
|
|
|
2024-01-04 14:51:35 +00:00
|
|
|
import "encoding/base64"
|
2024-01-04 13:21:19 +00:00
|
|
|
|
|
|
|
func UnmarshalBase64UrlJSON(data []byte) ([]byte, error) {
|
|
|
|
strData := string(data)
|
|
|
|
if len(strData) >= 2 && strData[0] == '"' && strData[len(strData)-1] == '"' {
|
|
|
|
strData = strData[1 : len(strData)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
if strData == "null" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2024-01-04 14:51:35 +00:00
|
|
|
decodedData, err := MultibaseDecodeString(strData)
|
|
|
|
if err != nil {
|
|
|
|
if err != ErrMultibaseEncodingNotSupported {
|
2024-01-04 13:21:19 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2024-01-04 14:51:35 +00:00
|
|
|
} else {
|
|
|
|
return decodedData, nil
|
2024-01-04 13:21:19 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 14:51:35 +00:00
|
|
|
decodedData, err = base64.RawURLEncoding.DecodeString(strData)
|
2024-01-04 13:21:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return decodedData, nil
|
|
|
|
}
|