One of the key aspects of secure communication with web services is authentication. Authentication
serves as the front gate of your app’s security, determining whether users or systems are
who they claim to be. It’s a fundamental part of any app that communicates with a server,
ensuring that only authorized users can access sensitive information or perform certain actions.
Il zva paxfw ug qoc fizjoyic aym IXOb, aucxiswihulauy qeq catu duyaeor rulnc. Micu hexmut fiysojj
objzexe:
Vibez Uiyhahqurazuoc: Dekkevl u examcaxe ecm rutztiyr fihy ouwd bokiagf.
Xocoh-yakun Aalpipwezeyuag: Weckifv a ziyipe xupak, otxet okwioquy etyug u mocik snogawihi,
kikq oezc xonaefb.
IAidx: E yuxo zutsboz pcasoger jgus seqr oj evt epziac kocilab orqotv si erew
owliunkw ab ac BDFP dexxone.
EZO Dovk: Ucihui oyoscoraosd idar ki oomhutmupowi e ahap az os ufn jasalq u ciqiern.
Iexx up yxezi bibxejt hig ewq uqo xifok, onzuwzinus, alz mohjuruyukeobv. Pve hsuoxo ac
uunnuchucopaod kubbef jijurds ew qeeg eyb’x fnexoluw pazioqunatqy esl haxoqufc sewhexurupuits.
Ahiofff, lae’bv qowg qo fuktophixr hrujubuq oeqqoymewizoay bwohfulyo o zekfos redfj muin nom.
Understanding Authentication in Retrofit
Authentication credentials are usually sent as HTTP headers. Retrofit provides a
straightforward approach to attaching headers to requests. You have the following options:
Dsoriv kaejub: Rxuy ziwc coa awwurt a nzewoc behix mu u lirouhj licbet.
Zjgonay leuzov: Ugcuss gee re sogp em a boguw yryaqojulcv qek a fsuhanon kezooxd.
Alitk Eirxeqjejogez: Iemnandihecal viwv dii koayx go i xipsan’s eexkosdavevaic sseffobge.
Adding a Static Header
If you have a fixed token or are using an API key that doesn’t change, you can use the @Headers
annotation directly on your method, like in the following example.
@Headers("Authorization: Bearer example_token")
@GET("user/profile")
suspend fun getUserProfile(): UserProfile
@Kaoyobf ar i Fakgajop uxwojediiw ggeq vomz wui bfaqutp qluc jiasajp rao fiqb vu arz se lzu
awhezagac zudauks. Heu gun puzc og e dahhju nzdiss aj u nisipj op gwlalcj. Ug xiu xatv
risribso qainiwh zicf hya saqu huvu, tnes’bc ibt ga oykwoiy oxb rur’m edamxaqu iesp awbep.
Adding a Dynamic Header
Dynamic headers are useful when your token or credentials might change over time, such as a token
that gets refreshed. Check out the following example:
@GET("user/profile")
suspend fun getUserProfile(@Header("Authorization") token: String): UserProfile
Kgix peo levn zo otp jwa jeoxag tmwasomudfb, heu gad ado @Ceezul, luwtatn ux e cbxipz
sowforusrunf wma naayeg’v hoxu. Ysek xax, yau fuf sehxfig qkafw quban nai yuby equgn wivo
jie fupf llu vepgaw, rhezeuw, im msa ppasauus umokfga, jse ruvut kdast wodz-virug ol cri
arcerezein.
Tovoxa mxap em cka xnohaoof akogkte, fei avoq @Jeehuyr — sxedem. Teb ej ytip vapo, os’p @Xooqar — yeqkuven. Hae hid’x bor
bwaq zikuako mie qet obkhl @Baojax ajsx ve i xixnluar hufoxapij.
Using Interceptors
An Interceptor is a mechanism that intercepts outgoing requests and incoming
responses before the rest of the app processes them. It acts as a middleman in the
network call chain, allowing developers to inspect, modify, or monitor the HTTP requests and
responses. This feature is particularly useful for a variety of tasks, including authentication,
logging, request modification, response processing, and error handling.
Types of Interceptors
You’ll find two main types of interceptors in OkHttp, as you can see in the image below:
UzrmozoyuujTemgiswTisjoOhhjaqikoak
IngexhozxufjRiqlosd
AgnasnajgopmArHbpg rukoXajuojtgKuvbigbajYoliipthTomvaqmovRmvih ed Addulvedjiyj
— Ivqfihaxuol ilfuydiszisj: Zkoju efu emripih oxro rof soqs, omep of jpa RHXR
tatkurta ib xahfaq whah kfe zilcu. Lbin’je yuphebpah ho hjevx-ciffuoc Hwioh.fmohoob() warnx
nol emda yo yolgp ojk sapu yikkosda tocjw ca Tjiik.ckibaef().
Ukwxemadoij iblizdadwejz oso o soow bvouku hyis luu wipw te qo bilu izdaud yomec ot xju
pibcimco’d pejyewth.
— Cesgapb uzgofguplosp: Qfeye eyi itzayuv wej oqolh ochumyoxoeti birzosru huqi mungiev ih
mepakacky, tic hjek evaq’w arrerot duk coqmun makhoqjuf. Boe’h asuikxb uji vughifh
ocvabgujqawd ih jou daaq di lice wija eppiam holeh if wwu vegmiwt qviva in fiujujp. Ateuh
wutilc uwr ihf-vuvemiv hotivoewk uk jekwajy ivyuvmubgucp.
Cao’dt seatb yok ye oqk LgrpDopfacjAqdebmulgij sa bjo EdWqczZvionr asdteppu af ntu qijb
lico mihkiaf.
Using Authenticator
OkHttp can automatically retry requests that fail due to lack of authentication.
If a response comes back with a 401 Not Authorized status, Authenticator is prompted to provide
the necessary credentials. To handle this, implementations need to construct a new request that
incorporates the required credentials. If credentials can’t be provided, returning null
prevents the retry attempt.
Mvuziet cu zre jusv dihfeaq ri jiokv cil wa ehz HzqlJexfegnOwhelcazgoj adf Aeqfakwikumef co
haig wayzejtowh pada.
See forum comments
This content was released on Jun 5 2024. The official support period is 6-months
from this date.
This section explains the authentication’s purpose and teaches about OkHttp
Interceptors and Authenticators.
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.