hasyrails / calendar-vue-original

0 stars 0 forks source link

145_api_url_viewer_contents / deviseのコールバックを実装する #316

Open hasyrails opened 3 years ago

hasyrails commented 3 years ago

現状

api/cardsなどでURLを叩くと apiでのレコードが見ることができてしまう。

→管理者にしか見れないよう実装する

ref #311

hasyrails commented 3 years ago

api/hogehogeのURLを叩いたとき、自分のidに一致するものしか見れないように実装すれば良い

hasyrails commented 3 years ago

current_api_userがnilのため設定できず??

hasyrails commented 3 years ago

同様の現象が公式GitHubにもあり

current_user and authenticate_user! returns false / devise_token_auth

current_user = User.find_by(uid: auth_headers["uid"])

cookies[:auth_headers]でググると発見

rails appのAPI urlに直接アクセスしてcurrent_userを取得するにはどうすればよいですか?私は認証にng-auth-tokenとdevise_token_authを使用しています。

# In ApplicationController 
def authenticate_current_user 
head :unauthorized if get_current_user.nil? 
end 

def get_current_user 
return nil unless cookies[:auth_headers] 
auth_headers = JSON.parse cookies[:auth_headers] 

expiration_datetime = DateTime.strptime(auth_headers["expiry"], "%s") 
current_user = User.find_by(uid: auth_headers["uid"]) 

if current_user && 
    current_user.tokens.has_key?(auth_headers["client"]) && 
    expiration_datetime > DateTime.now 

    @current_user = current_user 
end 
@current_user 
end 
hasyrails commented 3 years ago

アクセス制限完了 ログイン状態でも表示できないようにした Image from Gyazo

hasyrails commented 3 years ago

api/hogehoge にアクセスできない →表示もできない。

hasyrails commented 3 years ago

AWSのWAF で制限をかけることとする