Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
144 views
in Technique[技术] by (71.8m points)

android - Hide sensitive data inside application

I am using retrofit request:

@Headers("Authorization: Basic: UsernamePassword")
@POST("services/2/something/something")
fun createPlan(@Body planData: PlanInfo): Call<Plan>

In my header I requred to send usrname and password and string in Base64 encode. But it not so hard to decode if decomiling the app. So I refered to manual: https://medium.com/novumlogic/hiding-sensitive-data-in-android-app-dbd64e88224f for hiding sensitive data in android app.

But now when I use data from constant like so:

@Headers("Authorization: "+ConstantsEncrypted.usernamePassword())
@POST("services/2/something/something")
fun createPlan(@Body planData: PlanInfo): Call<Plan>

I get an error: An annotation argument must be a compile-time constant.

So how can I hide this sensitive data properly?

question from:https://stackoverflow.com/questions/65902204/hide-sensitive-data-inside-application

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You cannot do that using the annotation method. Here's how I have done it in my projects.

class Authenticator : Authenticator {

    override fun authenticate(route: Route?, response: Response): Request? {

        return response.request().newBuilder()
            .header("Authorization", ConstantsEncrypted.usernamePassword()).build()
    }

}

And don't forget to add it to your OkHttpClient.Builder() instance using authenticator(Authenticator()) something like below:

OkHttpClient.Builder().apply {
    writeTimeout(120, TimeUnit.SECONDS)
    readTimeout(120, TimeUnit.SECONDS)
    connectTimeout(120, TimeUnit.SECONDS)
    callTimeout(120, TimeUnit.SECONDS)
    // your code
    // this is where you add your authenticator
    authenticator(Authenticator())
    // more code 
}.build()

The Authenticator is from package okhttp3. You can find more info on class here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...