This project serves as a demo for implementing OAuth2 login functionality using multiple providers like Kakao, Naver, Google, and GitHub. Along with OAuth authentication, it also demonstrates how to create users in the database after successful authentication.
μ΄ νλ‘μ νΈλ μΉ΄μΉ΄μ€, λ€μ΄λ², ꡬκΈ, κΉνλΈμ κ°μ μ¬λ¬ μ 곡μ 체λ₯Ό μ¬μ©νμ¬ OAuth2 λ‘κ·ΈμΈ κΈ°λ₯μ ꡬννκΈ° μν λ°λͺ¨λ‘ μ¬μ©λμμ΅λλ€.
OAuth μΈμ¦ μΈμλ μΈμ¦μ΄ μ±κ³΅ν ν λ°μ΄ν°λ² μ΄μ€μ μ¬μ©μλ₯Ό μμ±νλ λ°©λ²λ 보μ¬μ€λλ€.
-
OAuth Authentication with multiple providers:
-
User Management: Creating users in the database post-authentication.
-
Token Management: JWT-based token issuance and validation for authenticated users.
-
Install mkcert:
First, you need to install
mkcert
for creating a local certificate for HTTPS development.On Windows (using Chocolatey):
choco install mkcert
On Linux:
sudo apt install libnss3-tools wget -O mkcert https://github.com/FiloSottile/mkcert/releases/download/v1.4.0/mkcert-v1.4.0-linux-amd64 chmod +x mkcert sudo mv mkcert /usr/local/bin/
Next, install the local CA:
mkcert -install
Finally, create a certificate for
localhost
:mkcert localhost
This will generate two files:
localhost.pem
(the certificate) andlocalhost-key.pem
(the private key). Remember the location of these files as you might need to reference them in your application for HTTPS. -
Clone the Repository:
git clone https://github.com/Alfex4936/rust-actix-oauth2.git cd rust-actix-oauth2
-
Setup Environment Variables:
Copy the sample
.env
content and set the appropriate values for your OAuth application:CLIENT_ORIGIN=http://localhost:3001 JWT_SECRET=your_secret ... # Fill other values accordingly.
-
Run the Application:
cargo install cargo-watch cargo watch -q -c -w src/ -x run
Hosted at
https://localhost:8080
. (Not onhttp://
)
Note: Ensure that your application is configured to use the generated localhost.pem
and localhost-key.pem
files when serving via HTTPS. This can often be done in the web framework's settings or configuration.
To get an insight into how the OAuth flow works, here's a snippet for Naver OAuth:
// ... [snip] ...
pub async fn get_naver_oauth_token(
authorization_code: &str,
data: &web::Data<AppState>,
) -> Result<BasicOauthToken, Box<dyn Error>> {
// ... [snip] ...
}
pub async fn get_naver_user(access_token: &str) -> Result<NaverUserResult, Box<dyn Error>> {
// ... [snip] ...
}
Let's make authentication easy for everyone!
This project is licensed under the MIT License - see the LICENSE file for details.