Skip to content

Commit

Permalink
add token validation for write endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
dfernandos committed Nov 8, 2023
1 parent bbd7970 commit 6769b7c
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 6 deletions.
71 changes: 71 additions & 0 deletions src/main/java/com/mapeando/territory/config/FirebaseConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.mapeando.territory.config;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Component
public class FirebaseConfig {

@Value("${firebase.type}")
private String type;

@Value("${firebase.project_id}")
private String project_id;

@Value("${firebase.private_key_id}")
private String private_key_id;

@Value("${firebase.private_key}")
private String private_key;

@Value("${firebase.client_email}")
private String client_email;

@Value("${firebase.client_id}")
private String client_id;

@Value("${firebase.auth_uri}")
private String auth_uri;

@Value("${firebase.token_uri}")
private String token_uri;

@Value("${firebase.auth_provider_x509_cert_url}")
private String auth_provider_x509_cert_url;

@Value("${firebase.client_x509_cert_url}")
private String client_x509_cert_url;

@Value("${firebase.universe_domain}")
private String universe_domain;

public String getPrivate_key(){
return private_key.replace("\\n", "\n");
}

@Override
public String toString() {
return "FirebaseConfig{" +
"type:'" + type + '\'' +
", project_id:'" + project_id + '\'' +
", private_key_id:'" + private_key_id + '\'' +
", private_key:'" + getPrivate_key() + '\'' +
", client_email:'" + client_email + '\'' +
", client_Id:'" + client_id + '\'' +
", auth_uri:'" + auth_uri + '\'' +
", token_uri:'" + token_uri + '\'' +
", auth_provider_x509_cert_url:'" + auth_provider_x509_cert_url + '\'' +
", client_x509_cert_url:'" + client_x509_cert_url + '\'' +
", universe_domain:'" + universe_domain + '\'' +
'}';
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.mapeando.territory.config;

import com.google.firebase.FirebaseException;
import com.google.firebase.auth.FirebaseAuth;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class FirebaseInterceptor implements HandlerInterceptor {

@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler) throws Exception{
try{
String idTokenParsed = httpServletRequest.getHeader("Authorization").substring(7);
FirebaseAuth.getInstance().verifyIdToken(idTokenParsed);
return true;

} catch (FirebaseException ex){
httpServletResponse.setStatus(403);
return false;
}
}
}
3 changes: 0 additions & 3 deletions src/main/resources/application-dev.yml

This file was deleted.

3 changes: 0 additions & 3 deletions src/main/resources/application-prod.yml

This file was deleted.

Empty file removed src/main/resources/application.yml
Empty file.

0 comments on commit 6769b7c

Please sign in to comment.