[{"data":1,"prerenderedAt":842},["ShallowReactive",2],{"$f_0q-HxDKH5xabViubBBtq9vdmSCGaJNz3WWVG_FzWB4":3},{"slug":4,"title":5,"description":6,"source":7,"document":8,"toc":826,"editUrl":836,"prev":837,"next":839},"techniques\u002Foauth2-oidc","OAuth2 & OIDC","Add social login with Google, GitHub and Discord using ferrite-auth-oauth.","---\ntitle: OAuth2 & OIDC\ndescription: Add social login with Google, GitHub and Discord using ferrite-auth-oauth.\n---\n\n# OAuth2 & OIDC\n\n`ferrite-auth-oauth` wires the full OAuth2 authorization-code flow for **Google**, **GitHub**, and **Discord** — including CSRF-protected state, token exchange, userinfo parsing, and JWT minting that is compatible with `ferrite-auth-jwt`.\n\n## Enable it\n\n```cargo\nfr add auth-oauth\n```\n\n```rust\n#[module(imports = [OAuthModule])]\npub struct AppModule;\n```\n\n## Provider configuration\n\nCreate an OAuth app per provider and set these env vars:\n\n| Provider | Vars |\n| --- | --- |\n| Google | `GOOGLE_OAUTH_CLIENT_ID`, `GOOGLE_OAUTH_CLIENT_SECRET`, `GOOGLE_OAUTH_REDIRECT_URI` |\n| GitHub | `GITHUB_OAUTH_CLIENT_ID`, `GITHUB_OAUTH_CLIENT_SECRET`, `GITHUB_OAUTH_REDIRECT_URI` |\n| Discord | `DISCORD_OAUTH_CLIENT_ID`, `DISCORD_OAUTH_CLIENT_SECRET`, `DISCORD_OAUTH_REDIRECT_URI` |\n\nOptional: `OAUTH_CLIENT_SUCCESS_URL` redirects the browser after login.\n\n## Routes\n\nThe module mounts two routes per provider:\n\n| Route | Purpose |\n| --- | --- |\n| `GET \u002Foauth\u002F{provider}\u002Fauthorize` | Redirects the user to the provider's consent screen |\n| `GET \u002Foauth\u002F{provider}\u002Fcallback` | Exchanges the code, returns `OAuthUser`, optionally redirects |\n\n```rust\nuse ferrite_auth_oauth::OAuthProvider;\n\nOAuthProvider::Google   \u002F\u002F \"google\"\nOAuthProvider::Github   \u002F\u002F \"github\"\nOAuthProvider::Discord  \u002F\u002F \"discord\"\n```\n\n`OAuthProvider` implements `Display`, `FromStr`, and lowercase serde.\n\n## `OAuthService`\n\n```rust\nuse ferrite_auth_oauth::{OAuthProvider, OAuthService};\n\n#[injectable]\npub struct AuthController {\n    oauth: OAuthService,\n}\n\nimpl AuthController {\n    #[inject]\n    pub fn new(oauth: OAuthService) -> Self {\n        Self { oauth }\n    }\n\n    pub async fn start(&self, provider: OAuthProvider) -> Result\u003CString, crate::Error> {\n        Ok(self.oauth.authorize_url(provider)?)\n    }\n}\n```\n\n| Method | Returns |\n| --- | --- |\n| `provider_config(provider)` | The resolved `ProviderConfig` |\n| `authorize_url(provider)` | The consent-screen URL (CSRF state embedded) |\n| `callback(provider, code, state, error)` | `OAuthUser` after exchanging the code |\n| `sign_jwt(&OAuthUser)` | A Ferrite-JWT-compatible HS256 token |\n\nThe callback validates the signed state (HMAC-SHA256 over a random nonce; secret from `JWT_SECRET` or `OAUTH_STATE_SECRET`) — protecting against login CSRF.\n\n## `OAuthUser`\n\n```rust\npub struct OAuthUser {\n    pub id: String,\n    pub email: String,\n    pub name: String,\n    pub avatar_url: String,\n    pub provider: OAuthProvider,\n}\n```\n\n## JWT hand-off\n\nBecause the tokens are HS256-compatible with `ferrite-auth-jwt`, you can sign a session token and pass it through:\n\n```rust\nlet user = oauth.callback(provider, code, state, error)?;\nlet token = oauth.sign_jwt(&user)?;\n\u002F\u002F redirect the browser with token=...\n```\n\nThe `sub` claim maps to the OAuth user id (`String` → serialized), and `email` is the user's email.\n\n## `OAuthError`\n\nDistinct variants keep failure modes debug-friendly: `UnknownProvider`, `MissingConfig{prefix}`, `StateSign`, `InvalidState`, `Http`, `ProviderStatus`, `Decode`, `Authorization`, `Jwt`.\n\n## Next steps\n\n- [Authentication (JWT)](\u002Fdocs\u002Ftechniques\u002Fauthentication).\n- [Configuration](\u002Fdocs\u002Foverview\u002Fconfiguration).",{"frontmatter":9,"meta":10,"nodes":40},{"title":5,"description":6},{"toc":11},{"title":12,"searchDepth":13,"depth":14,"links":15},"On This Page",2,3,[16,19,22,25,28,31,34,37],{"id":17,"depth":13,"text":18},"enable-it","Enable it",{"id":20,"depth":13,"text":21},"provider-configuration","Provider configuration",{"id":23,"depth":13,"text":24},"routes","Routes",{"id":26,"depth":13,"text":27},"oauthservice","OAuthService",{"id":29,"depth":13,"text":30},"oauthuser","OAuthUser",{"id":32,"depth":13,"text":33},"jwt-hand-off","JWT hand-off",{"id":35,"depth":13,"text":36},"oautherror","OAuthError",{"id":38,"depth":13,"text":39},"next-steps","Next steps",[41,45,70,73,82,127,129,132,197,204,206,209,246,303,315,319,531,590,601,605,690,692,698,759,773,777,807,809],[42,43,5],"h1",{"id":44},"oauth2-oidc",[46,47,48,52,53,57,58,61,62,65,66,69],"p",{},[49,50,51],"code",{},"ferrite-auth-oauth"," wires the full OAuth2 authorization-code flow for ",[54,55,56],"strong",{},"Google",", ",[54,59,60],{},"GitHub",", and ",[54,63,64],{},"Discord"," — including CSRF-protected state, token exchange, userinfo parsing, and JWT minting that is compatible with ",[49,67,68],{},"ferrite-auth-jwt",".",[71,72,18],"h2",{"id":17},[74,75,78],"pre",{"language":76,"class":77},"cargo","shiki dark:github-dark",[49,79,81],{"class":80},"language-cargo","fr add auth-oauth",[74,83,86],{"language":84,"class":85},"rust","shiki shiki-themes github-light github-dark dark:github-dark",[49,87,89,112,113],{"class":88},"language-rust",[90,91,94,98,102,105,109],"span",{"class":92,"style":93},"line","display: inline",[90,95,97],{"style":96},"color:#24292E;--shiki-dark:#E1E4E8","#[module(imports ",[90,99,101],{"style":100},"color:#D73A49;--shiki-dark:#F97583","=",[90,103,104],{"style":96}," [",[90,106,108],{"style":107},"color:#6F42C1;--shiki-dark:#B392F0","OAuthModule",[90,110,111],{"style":96},"])]","\n",[90,114,115,118,121,124],{"class":92,"style":93},[90,116,117],{"style":100},"pub",[90,119,120],{"style":100}," struct",[90,122,123],{"style":107}," AppModule",[90,125,126],{"style":96},";",[71,128,21],{"id":20},[46,130,131],{},"Create an OAuth app per provider and set these env vars:",[133,134,135,148],"table",{},[136,137,138],"thead",{},[139,140,141,145],"tr",{},[142,143,144],"th",{},"Provider",[142,146,147],{},"Vars",[149,150,151,167,182],"tbody",{},[139,152,153,156],{},[154,155,56],"td",{},[154,157,158,57,161,57,164],{},[49,159,160],{},"GOOGLE_OAUTH_CLIENT_ID",[49,162,163],{},"GOOGLE_OAUTH_CLIENT_SECRET",[49,165,166],{},"GOOGLE_OAUTH_REDIRECT_URI",[139,168,169,171],{},[154,170,60],{},[154,172,173,57,176,57,179],{},[49,174,175],{},"GITHUB_OAUTH_CLIENT_ID",[49,177,178],{},"GITHUB_OAUTH_CLIENT_SECRET",[49,180,181],{},"GITHUB_OAUTH_REDIRECT_URI",[139,183,184,186],{},[154,185,64],{},[154,187,188,57,191,57,194],{},[49,189,190],{},"DISCORD_OAUTH_CLIENT_ID",[49,192,193],{},"DISCORD_OAUTH_CLIENT_SECRET",[49,195,196],{},"DISCORD_OAUTH_REDIRECT_URI",[46,198,199,200,203],{},"Optional: ",[49,201,202],{},"OAUTH_CLIENT_SUCCESS_URL"," redirects the browser after login.",[71,205,24],{"id":23},[46,207,208],{},"The module mounts two routes per provider:",[133,210,211,221],{},[136,212,213],{},[139,214,215,218],{},[142,216,217],{},"Route",[142,219,220],{},"Purpose",[149,222,223,233],{},[139,224,225,230],{},[154,226,227],{},[49,228,229],{},"GET \u002Foauth\u002F{provider}\u002Fauthorize",[154,231,232],{},"Redirects the user to the provider's consent screen",[139,234,235,240],{},[154,236,237],{},[49,238,239],{},"GET \u002Foauth\u002F{provider}\u002Fcallback",[154,241,242,243,245],{},"Exchanges the code, returns ",[49,244,30],{},", optionally redirects",[74,247,248],{"language":84,"class":85},[49,249,250,112,266,112,268,112,280,112,292],{"class":88},[90,251,252,255,258,261,264],{"class":92,"style":93},[90,253,254],{"style":100},"use",[90,256,257],{"style":107}," ferrite_auth_oauth",[90,259,260],{"style":100},"::",[90,262,263],{"style":107},"OAuthProvider",[90,265,126],{"style":96},[90,267],{"class":92,"style":93},[90,269,270,272,274,276],{"class":92,"style":93},[90,271,263],{"style":107},[90,273,260],{"style":100},[90,275,56],{"style":107},[90,277,279],{"style":278},"color:#6A737D;--shiki-dark:#6A737D","   \u002F\u002F \"google\"",[90,281,282,284,286,289],{"class":92,"style":93},[90,283,263],{"style":107},[90,285,260],{"style":100},[90,287,288],{"style":107},"Github",[90,290,291],{"style":278},"   \u002F\u002F \"github\"",[90,293,294,296,298,300],{"class":92,"style":93},[90,295,263],{"style":107},[90,297,260],{"style":100},[90,299,64],{"style":107},[90,301,302],{"style":278},"  \u002F\u002F \"discord\"",[46,304,305,307,308,57,311,314],{},[49,306,263],{}," implements ",[49,309,310],{},"Display",[49,312,313],{},"FromStr",", and lowercase serde.",[71,316,317],{"id":26},[49,318,27],{},[74,320,321],{"language":84,"class":85},[49,322,323,112,343,112,345,112,350,112,362,112,376,112,381,112,383,112,392,112,397,112,427,112,435,112,440,112,442,112,495,112,523,112,527],{"class":88},[90,324,325,327,329,331,334,336,338,340],{"class":92,"style":93},[90,326,254],{"style":100},[90,328,257],{"style":107},[90,330,260],{"style":100},[90,332,333],{"style":96},"{",[90,335,263],{"style":107},[90,337,57],{"style":96},[90,339,27],{"style":107},[90,341,342],{"style":96},"};",[90,344],{"class":92,"style":93},[90,346,347],{"class":92,"style":93},[90,348,349],{"style":96},"#[injectable]",[90,351,352,354,356,359],{"class":92,"style":93},[90,353,117],{"style":100},[90,355,120],{"style":100},[90,357,358],{"style":107}," AuthController",[90,360,361],{"style":96}," {",[90,363,364,367,370,373],{"class":92,"style":93},[90,365,366],{"style":96},"    oauth",[90,368,369],{"style":100},":",[90,371,372],{"style":107}," OAuthService",[90,374,375],{"style":96},",",[90,377,378],{"class":92,"style":93},[90,379,380],{"style":96},"}",[90,382],{"class":92,"style":93},[90,384,385,388,390],{"class":92,"style":93},[90,386,387],{"style":100},"impl",[90,389,358],{"style":107},[90,391,361],{"style":96},[90,393,394],{"class":92,"style":93},[90,395,396],{"style":96},"    #[inject]",[90,398,399,402,405,408,411,413,415,418,421,425],{"class":92,"style":93},[90,400,401],{"style":100},"    pub",[90,403,404],{"style":100}," fn",[90,406,407],{"style":107}," new",[90,409,410],{"style":96},"(oauth",[90,412,369],{"style":100},[90,414,372],{"style":107},[90,416,417],{"style":96},") ",[90,419,420],{"style":100},"->",[90,422,424],{"style":423},"color:#005CC5;--shiki-dark:#79B8FF"," Self",[90,426,361],{"style":96},[90,428,429,432],{"class":92,"style":93},[90,430,431],{"style":423},"        Self",[90,433,434],{"style":96}," { oauth }",[90,436,437],{"class":92,"style":93},[90,438,439],{"style":96},"    }",[90,441],{"class":92,"style":93},[90,443,444,446,449,451,454,457,460,463,466,468,471,473,475,478,481,484,486,489,492],{"class":92,"style":93},[90,445,401],{"style":100},[90,447,448],{"style":100}," async",[90,450,404],{"style":100},[90,452,453],{"style":107}," start",[90,455,456],{"style":96},"(",[90,458,459],{"style":100},"&",[90,461,462],{"style":423},"self",[90,464,465],{"style":96},", provider",[90,467,369],{"style":100},[90,469,470],{"style":107}," OAuthProvider",[90,472,417],{"style":96},[90,474,420],{"style":100},[90,476,477],{"style":107}," Result",[90,479,480],{"style":96},"\u003C",[90,482,483],{"style":107},"String",[90,485,57],{"style":96},[90,487,488],{"style":100},"crate::",[90,490,491],{"style":107},"Error",[90,493,494],{"style":96},"> {",[90,496,497,500,502,504,506,509,511,514,517,520],{"class":92,"style":93},[90,498,499],{"style":107},"        Ok",[90,501,456],{"style":96},[90,503,462],{"style":423},[90,505,69],{"style":100},[90,507,508],{"style":96},"oauth",[90,510,69],{"style":100},[90,512,513],{"style":107},"authorize_url",[90,515,516],{"style":96},"(provider)",[90,518,519],{"style":100},"?",[90,521,522],{"style":96},")",[90,524,525],{"class":92,"style":93},[90,526,439],{"style":96},[90,528,529],{"class":92,"style":93},[90,530,380],{"style":96},[133,532,533,543],{},[136,534,535],{},[139,536,537,540],{},[142,538,539],{},"Method",[142,541,542],{},"Returns",[149,544,545,558,568,580],{},[139,546,547,552],{},[154,548,549],{},[49,550,551],{},"provider_config(provider)",[154,553,554,555],{},"The resolved ",[49,556,557],{},"ProviderConfig",[139,559,560,565],{},[154,561,562],{},[49,563,564],{},"authorize_url(provider)",[154,566,567],{},"The consent-screen URL (CSRF state embedded)",[139,569,570,575],{},[154,571,572],{},[49,573,574],{},"callback(provider, code, state, error)",[154,576,577,579],{},[49,578,30],{}," after exchanging the code",[139,581,582,587],{},[154,583,584],{},[49,585,586],{},"sign_jwt(&OAuthUser)",[154,588,589],{},"A Ferrite-JWT-compatible HS256 token",[46,591,592,593,596,597,600],{},"The callback validates the signed state (HMAC-SHA256 over a random nonce; secret from ",[49,594,595],{},"JWT_SECRET"," or ",[49,598,599],{},"OAUTH_STATE_SECRET",") — protecting against login CSRF.",[71,602,603],{"id":29},[49,604,30],{},[74,606,607],{"language":84,"class":85},[49,608,609,112,620,112,634,112,647,112,660,112,673,112,686],{"class":88},[90,610,611,613,615,618],{"class":92,"style":93},[90,612,117],{"style":100},[90,614,120],{"style":100},[90,616,617],{"style":107}," OAuthUser",[90,619,361],{"style":96},[90,621,622,624,627,629,632],{"class":92,"style":93},[90,623,401],{"style":100},[90,625,626],{"style":96}," id",[90,628,369],{"style":100},[90,630,631],{"style":107}," String",[90,633,375],{"style":96},[90,635,636,638,641,643,645],{"class":92,"style":93},[90,637,401],{"style":100},[90,639,640],{"style":96}," email",[90,642,369],{"style":100},[90,644,631],{"style":107},[90,646,375],{"style":96},[90,648,649,651,654,656,658],{"class":92,"style":93},[90,650,401],{"style":100},[90,652,653],{"style":96}," name",[90,655,369],{"style":100},[90,657,631],{"style":107},[90,659,375],{"style":96},[90,661,662,664,667,669,671],{"class":92,"style":93},[90,663,401],{"style":100},[90,665,666],{"style":96}," avatar_url",[90,668,369],{"style":100},[90,670,631],{"style":107},[90,672,375],{"style":96},[90,674,675,677,680,682,684],{"class":92,"style":93},[90,676,401],{"style":100},[90,678,679],{"style":96}," provider",[90,681,369],{"style":100},[90,683,470],{"style":107},[90,685,375],{"style":96},[90,687,688],{"class":92,"style":93},[90,689,380],{"style":96},[71,691,33],{"id":32},[46,693,694,695,697],{},"Because the tokens are HS256-compatible with ",[49,696,68],{},", you can sign a session token and pass it through:",[74,699,700],{"language":84,"class":85},[49,701,702,112,727,112,754],{"class":88},[90,703,704,707,710,712,715,717,720,723,725],{"class":92,"style":93},[90,705,706],{"style":100},"let",[90,708,709],{"style":96}," user ",[90,711,101],{"style":100},[90,713,714],{"style":96}," oauth",[90,716,69],{"style":100},[90,718,719],{"style":107},"callback",[90,721,722],{"style":96},"(provider, code, state, error)",[90,724,519],{"style":100},[90,726,126],{"style":96},[90,728,729,731,734,736,738,740,743,745,747,750,752],{"class":92,"style":93},[90,730,706],{"style":100},[90,732,733],{"style":96}," token ",[90,735,101],{"style":100},[90,737,714],{"style":96},[90,739,69],{"style":100},[90,741,742],{"style":107},"sign_jwt",[90,744,456],{"style":96},[90,746,459],{"style":100},[90,748,749],{"style":96},"user)",[90,751,519],{"style":100},[90,753,126],{"style":96},[90,755,756],{"class":92,"style":93},[90,757,758],{"style":278},"\u002F\u002F redirect the browser with token=...",[46,760,761,762,765,766,768,769,772],{},"The ",[49,763,764],{},"sub"," claim maps to the OAuth user id (",[49,767,483],{}," → serialized), and ",[49,770,771],{},"email"," is the user's email.",[71,774,775],{"id":35},[49,776,36],{},[46,778,779,780,57,783,57,786,57,789,57,792,57,795,57,798,57,801,57,804,69],{},"Distinct variants keep failure modes debug-friendly: ",[49,781,782],{},"UnknownProvider",[49,784,785],{},"MissingConfig{prefix}",[49,787,788],{},"StateSign",[49,790,791],{},"InvalidState",[49,793,794],{},"Http",[49,796,797],{},"ProviderStatus",[49,799,800],{},"Decode",[49,802,803],{},"Authorization",[49,805,806],{},"Jwt",[71,808,39],{"id":38},[810,811,812,820],"ul",{},[813,814,815,69],"li",{},[816,817,819],"a",{"href":818},"\u002Fdocs\u002Ftechniques\u002Fauthentication","Authentication (JWT)",[813,821,822,69],{},[816,823,825],{"href":824},"\u002Fdocs\u002Foverview\u002Fconfiguration","Configuration",{"title":12,"searchDepth":13,"depth":14,"links":827},[828,829,830,831,832,833,834,835],{"id":17,"depth":13,"text":18},{"id":20,"depth":13,"text":21},{"id":23,"depth":13,"text":24},{"id":26,"depth":13,"text":27},{"id":29,"depth":13,"text":30},{"id":32,"depth":13,"text":33},{"id":35,"depth":13,"text":36},{"id":38,"depth":13,"text":39},"https:\u002F\u002Fgithub.com\u002Fj4flmao\u002Fferrite_rs\u002Fedit\u002Fmain\u002Fdocs\u002Fcontent\u002Ftechniques\u002Foauth2-oidc.md",{"title":819,"slug":838},"techniques\u002Fauthentication",{"title":840,"slug":841},"Caching (Redis)","techniques\u002Fcaching",1789565439199]