@@ -27,77 +27,10 @@ package com.looker.rtl
27
27
import com.looker.sdk.AccessToken
28
28
import java.security.MessageDigest
29
29
import java.security.SecureRandom
30
- import kotlin.experimental.and
30
+ import java.util.Base64
31
31
32
- // https://stackoverflow.com/a/52225984/74137
33
- // TODO performance comparison of these two methods
34
- @ExperimentalUnsignedTypes
35
- fun ByteArray.toHexStr () = asUByteArray().joinToString(" " ) { it.toString(16 ).padStart(2 , ' 0' ) }
36
-
37
- // Adapted from https://www.samclarke.com/kotlin-hash-strings/
38
-
39
- fun String.md5 (): String {
40
- return hashString(this , " MD5" )
41
- }
42
-
43
- fun String.sha512 (): String {
44
- return hashString(this , " SHA-512" )
45
- }
46
-
47
- fun String.sha256 (): String {
48
- return hashString(this , " SHA-256" )
49
- }
50
-
51
- fun String.sha1 (): String {
52
- return hashString(this , " SHA-1" )
53
- }
54
-
55
- fun hashString (input : ByteArray , digester : MessageDigest ): String {
56
- val HEX_CHARS = " 0123456789abcdef"
57
- val bytes = digester
58
- .digest(input)
59
- val result = StringBuilder (bytes.size * 2 )
60
-
61
- bytes.forEach {
62
- val i = it.toInt()
63
- result.append(HEX_CHARS [i shr 4 and 0x0f ])
64
- result.append(HEX_CHARS [i and 0x0f ])
65
- }
66
-
67
- return result.toString()
68
- }
69
-
70
- fun hashString (input : ByteArray , type : String ): String {
71
- val digester = MessageDigest .getInstance(type)
72
- return hashString(input, digester)
73
- }
74
-
75
- /* *
76
- * Supported algorithms on Android:
77
- *
78
- * Algorithm Supported API Levels
79
- * MD5 1+
80
- * SHA-1 1+
81
- * SHA-224 1-8,22+
82
- * SHA-256 1+
83
- * SHA-384 1+
84
- * SHA-512 1+
85
- */
86
- fun hashString (input : String , type : String ): String {
87
- return hashString(input.toByteArray(), type)
88
- }
89
-
90
- private val hexArray = " 0123456789abcdef" .toCharArray()
91
-
92
- fun hexStr (bytes : ByteArray ): String {
93
- val hexChars = CharArray (bytes.size * 2 )
94
- for (j in bytes.indices) {
95
- val v = (bytes[j] and 0xFF .toByte()).toInt()
96
-
97
- hexChars[j * 2 ] = hexArray[v ushr 4 ]
98
- hexChars[j * 2 + 1 ] = hexArray[v and 0x0F ]
99
- }
100
- return String (hexChars)
32
+ fun base64UrlEncode (bytes : ByteArray ): String {
33
+ return Base64 .getUrlEncoder().encodeToString(bytes)
101
34
}
102
35
103
36
@ExperimentalUnsignedTypes
@@ -142,7 +75,7 @@ class OAuthSession(override val apiSettings: ConfigurationProvider, override val
142
75
* Generate an OAuth2 authCode request URL
143
76
*/
144
77
fun createAuthCodeRequestUrl (scope : String , state : String ): String {
145
- this .codeVerifier = this .secureRandom(32 ).toHexStr( )
78
+ this .codeVerifier = base64UrlEncode( this .secureRandom(32 ))
146
79
val codeChallenge = this .sha256hash(this .codeVerifier)
147
80
val config = this .apiSettings.readConfig()
148
81
val lookerUrl = config[" looker_url" ]
@@ -160,14 +93,13 @@ class OAuthSession(override val apiSettings: ConfigurationProvider, override val
160
93
fun redeemAuthCodeBody (authCode : String , codeVerifier : String? = null): Map <String , String > {
161
94
val verifier = codeVerifier? : this .codeVerifier
162
95
val config = this .apiSettings.readConfig()
163
- val map = mapOf (
96
+ return mapOf (
164
97
" grant_type" to " authorization_code" ,
165
98
" code" to authCode,
166
99
" code_verifier" to verifier,
167
100
" client_id" to (config[" client_id" ] ? : error(" " )),
168
101
" redirect_uri" to (config[" redirect_uri" ] ? : error(" " ))
169
102
)
170
- return map
171
103
}
172
104
173
105
fun redeemAuthCode (authCode : String , codeVerifier : String? = null): AuthToken {
@@ -181,7 +113,8 @@ class OAuthSession(override val apiSettings: ConfigurationProvider, override val
181
113
}
182
114
183
115
fun sha256hash (value : ByteArray ): String {
184
- return hashString(value, messageDigest)
116
+ val bytes = messageDigest.digest(value)
117
+ return base64UrlEncode(bytes)
185
118
}
186
119
187
120
fun sha256hash (value : String ): String {
0 commit comments